Reputation: 4359
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?
Upvotes: 30
Views: 30625
Reputation: 1157
According to the documentation, the retention period for logs is determined by the retainedFileCountLimit
setting. By default, retainedFileCountLimit
has a value of 31 so only the most recent 31 log files are kept.
When configuring Serilog
programmatically, you can adjust the number of files retained by specifying the retainedFileCountLimit
parameter as follows:
var log = new LoggerConfiguration()
.WriteTo.File("log.txt", retainedFileCountLimit: 42)
.CreateLogger();
pass null
to remove the limit.
When configuring Serilog
using XML (in the <appSettings>
):
<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.
When configuring Serilog
using JSON (in an appsettings.json
file):
{
"Serilog": {
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "log.txt",
"retainedFileCountLimit": "42"
}
}
]
}
}
and pass an empty string to remove the limit.
Upvotes: 34
Reputation: 1380
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
Upvotes: 7
Reputation: 415
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
Upvotes: 10