TechNerd TechNerd - 2 months ago 20
C# Question

Serilog RollingFile

I am trying to use WriteTo.RollingFile with Serilog as the following:

var log = new LoggerConfiguration().WriteTo.RollingFile(
@"F:\logs\log-{Date}.txt",
LogEventLevel.Debug).CreateLogger();
log.Information("this is a log test");


My understanding is that the log file will be created and named based on the date, and also it will write to a new file everyday, however I am getting a new log file for each log entry during the same day!
How to configure Serilog to write to a new file every day so ideally I have a single log file per day?

And is there any archiving process to delete files older than 7 days?

Answer

Try below:

 var log = new LoggerConfiguration()
          .MinimumLevel.Debug()
          .WriteTo.RollingFile(@"f:\log\log.txt", retainedFileCountLimit:7) 
          .CreateLogger();

The log file name will be automatically log-20150819.txt etc. You do not need to specify the date.