Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure Serilog File sink to use one log file per application run

I want to configure Serilog to create one log file per app run.
File name should be based on current date/time:
1st run: log_20180413_1020.txt
2nd run: log_20180413_1033.txt

I didn't find how to do it in any File sink (Rolling, Alternative..)
Any clue?

like image 489
IgorStack Avatar asked Apr 13 '18 17:04

IgorStack


People also ask

What is sink in Serilog?

Serilog provides sinks for writing log events to storage in various formats. Many of the sinks listed below are developed and supported by the wider Serilog community; please direct questions and issues to the relevant repository. More sinks can be found by searching within the serilog tag on NuGet.

Where does Serilog write to by default?

By default, serilog will only log to the console.

Is Serilog deprecated?

This package has been deprecated as it is legacy and is no longer maintained.

What is Serilog retainedFileCountLimit?

Parameter retainedFileCountLimit tells Serilog how many log files you want in any given time. Here it is set to 10. So on 11th day, Serilog will be generating a log as usual. But to keep the log files count to 10, it will delete the day 1 log file.


1 Answers

Configure the Serilog.Sinks.File sink without any rolling period or size limit, and add the timestamp to the log filename when you configure logging at startup:

string timestamp = DateTime.Now.ToString("yyyyMMdd_HHmm");
var log = new LoggerConfiguration()
    .WriteTo.File($"log{timestamp}.txt")
    .CreateLogger();

Per the Rolling Policies section of the README:

Specifying both rollingInterval and rollOnFileSizeLimit will cause both policies to be applied, while specifying neither will result in all events being written to a single file.

like image 156
McGuireV10 Avatar answered Oct 06 '22 18:10

McGuireV10