How do I automatically tail (delete) older logs using Serilog in a .Net WPF application?

According to https://github.com/serilog/serilog-sinks-file/blob/dev/README.md the default value of retainedFileCountLimit is 31 so only the most recent 31 files are kept by default.

To change the amount of files kept in code:

var log = new LoggerConfiguration()
    .WriteTo.File("log.txt", retainedFileCountLimit:= 42)
    .CreateLogger();

pass null to remove the limit.

In XML <appSettings> configuration:

<appSettings>
  <add key="serilog:using:File" value="Serilog.Sinks.File" />
  <add key="serilog:write-to:File.path" value="log.txt" />
  <add key="serilog:write-to:File.retainedFileCountLimit" value="42"/>
</appSettings>

and pass an empty string to remove the limit.

In JSON appsettings.json configuration

{
  "Serilog": {
    "WriteTo": [
      { "Name": "File", "Args": { "path": "log.txt", "retainedFileCountLimit": "42" } }
    ]
  }
}

and pass an empty string to remove the limit. Note that I have not tested the JSON configuration.


https://github.com/serilog/serilog-sinks-rollingfile/blob/dev/README.md Look there. You can configure autocreation of a new log file every day and also you can set how many of them you want to be kept


Now you can also specify a property retainedFileTimeLimit: https://github.com/serilog/serilog-sinks-file/pull/90

By the way, don't forget to specify retainedFileCountLimit: null if you want limitation only by the date. With the current implementation default value of retainedFileCountLimit is 31. Therefore, if you leave the parameter out, this filter will also be applied

Tags:

C#

.Net

Wpf

Serilog