Serilog RollingFile

TechNerd picture TechNerd · Aug 20, 2015 · Viewed 45k times · Source

I am trying to use WriteTo.RollingFile with Serilog as the following to write one file per day:

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

But I am getting a new log file for each log entry during the same day!

How does one configure Serilog to write to a new file every day as to have a have a single log file per day?


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

Answer

Infinity Challenger picture Infinity Challenger · Aug 20, 2015

Try below:

 var log = new LoggerConfiguration()
          .MinimumLevel.Debug()
          .WriteTo.File(@"f:\log\log.txt", rollingInterval: RollingInterval.Day) 
          .CreateLogger();

The log file name will be automatically log-20150819.txt etc. You do not need to specify the date.Old files will be cleaned up as per retainedFileCountLimit - default is 31.