John-Luke Laue
John-Luke Laue

Reputation: 3856

Create a new file with an updated suffix every time a new Logger is created with Serilog

I would like to have Serilog write to a new file with an incremented suffix every time a new Logger is created.

For example:

Basically, I want the behavior posted by the OP on this github issue: https://github.com/serilog/serilog-sinks-rollingfile/issues/9

I want was the OP was trying to get rid of.

When I use this code below, only one file is generated even though the logger is newed up multiple times:

var log = new LoggerConfiguration()
    .WriteTo.RollingFile(@"C:\temp\testlog\log-{Date}.txt")
    .CreateLogger();

log.Debug("Created logger...");

Upvotes: 2

Views: 3558

Answers (1)

Edward
Edward

Reputation: 30016

I suggest you check whether the generated one file contains Created logger....

For LoggerConfiguration, its default LogEventLevel is Serilog.Events.LogEventLevel.Information which means your log.Debug("Created logger..."); code will not create log records.

I suggest you make a test with code below:

        var log = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .WriteTo.RollingFile(@"C:\Windows\Temp\testlog\log-{Date}.txt")
                .CreateLogger();

        log.Debug("Created logger1...");

        var log1 = new LoggerConfiguration()
                 .MinimumLevel.Debug()
                .WriteTo.RollingFile(@"C:\Windows\Temp\testlog\log-{Date}.txt", Serilog.Events.LogEventLevel.Debug)
                .CreateLogger();

        log1.Debug("Created logger2...");

Above code will create two files, if you require the file name exactly like log_001.txt and log_002.txt, you need to implement your own sink, check this discussion Filename without date #59.

Upvotes: 3

Related Questions