I was trying to clear log files periodically. So, while searching in serilog.sink.file v5.0.0 I found out a something called retainedFileTimeLimit and I tried it but it didn't work.
This is my code ->
static void Main(string[] args)
{
var _loggger = new LoggerConfiguration()
.WriteTo.File(
new JsonFormatter(),
filePath,
Serilog.Events.LogEventLevel.Information,
retainedFileTimeLimit: TimeSpan.FromMinutes(2),
fileSizeLimitBytes: 10000,
rollOnFileSizeLimit: true,
retainedFileCountLimit: 40,
flushToDiskInterval: TimeSpan.FromSeconds(1),
rollingInterval: RollingInterval.Day
)
.CreateLogger();
for ( int i = 0; i < 20; i++ )
{
_loggger.Information("Logging Info");
_loggger.Warning("Logging Warning");
}
}
It deletes the file once it reaches its fileSizeLimitBytes: 10000 and creates a new file even thought I'm still inside the 10 minutes time frame and my retainedFileCountLimit: 50 and rollingInterval: RollingInterval.Day.
But, when I make retainedFileTimeLimit: TimeSpan.FromDays(10) it creates a new file once the file size limit exceeds and it doesn't delete the old file.
I searched about this everywhere but I didn't find any thing but this GitHub issue
in serilog src, you can find a method calls "ShouldRetainFile", which filter logs file to be deleted. it must be "index >= _retainedFileCountLimit.Value - 1" and "file.DateTime.Value < now.Subtract(_retainedFileTimeLimit.Value)"