28

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?

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
JohnB
  • 3,921
  • 8
  • 49
  • 99

3 Answers3

29

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.

somethingRandom
  • 811
  • 11
  • 16
9

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

Kirhgoph
  • 405
  • 5
  • 10
  • So essentially the answer is NO, according to that. – shawn1874 Jan 10 '18 at 20:47
  • 2
    Well you can configure rolling file for once a day and then set `retainedFileCountLimit` to desired days limit... – Jakoss Sep 10 '18 at 05:53
  • @WoIIe it is indeed not a direct answer to the question but it solves the problem of deleting old logs – Kirhgoph Apr 11 '19 at 12:45
  • 6
    This answer a) relies on an external link rather than copying relevant information, and b) is somewhat outdated as the "rollingfile" sink has been merged into the regular "file" sync. I recommend looking at the next answer. – TheRubberDuck Jul 01 '20 at 16:49
  • yup serilog have default implementation for deleting older log files. if we set the rollingInterval to day, it will only keep the last 31 days logs(which is configurable). – Surendra Shrestha Jun 09 '21 at 16:35
7

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

mihails.kuzmins
  • 1,140
  • 1
  • 11
  • 19