Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

wpf

serilog

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?

like image 551
JohnB Avatar asked Jun 15 '17 21:06

JohnB


People also ask

What is Rollinginterval in Serilog?

Specifies the frequency at which the log file should roll. Namespace: Serilog.Sinks.AmazonS3.

What is Serilog retainedFileCountLimit?

Parameter retainedFileCountLimit tells Serilog how many log files you want in any given time. Here it is set to 10. So on 11th day, Serilog will be generating a log as usual. But to keep the log files count to 10, it will delete the day 1 log file.

What is Serilog sink?

Serilog sink that writes to console with high-performance non-blocking output. Supports plaintext and JSON output but does not support themes and colors. This sink uses a background channel with a single text buffer and async writes to remove all blocking and lock contention to the console output stream.


2 Answers

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.

like image 102
somethingRandom Avatar answered Oct 19 '22 16:10

somethingRandom


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

like image 34
Kirhgoph Avatar answered Oct 19 '22 18:10

Kirhgoph