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

I'm using Serliog in a .Net WPF application.

Is there a way that I can "tail" (delete) the log files automatically when they are over N days old?



Solution 1:[1]

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

Solution 2:[2]

According to the documentation, 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.

Solution 3:[3]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kirhgoph
Solution 2
Solution 3