Serilog retainedFileTimeLimit .net 6 is not working

336 Views Asked by At

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

2

There are 2 best solutions below

1
dumbnessrf On

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)"

bool ShouldRetainFile(RollingLogFile file, int index, DateTime now)
{
    if (_retainedFileCountLimit.HasValue && index >= _retainedFileCountLimit.Value - 1)
        return false;

    if (_retainedFileTimeLimit.HasValue && file.DateTime.HasValue &&
        file.DateTime.Value < now.Subtract(_retainedFileTimeLimit.Value))
    {
        return false;
    }

return true;
}
1
dumbnessrf On

maybe you can delete logs folder by youself

            Task.Run(() =>
            {
                while (true)
                {
                    var folders = Directory.GetDirectories(logsFolder);
                    foreach (var folder in folders)
                    {
                        var sw = Stopwatch.StartNew();
                        var dir = new DirectoryInfo(folder);
                        if (dir.CreationTime.AddDays(3) < DateTime.Now) //3 days ago
                        {
                            sw.Restart();
                            Directory.Delete(folder, true);
                            sw.Stop();
                            logger?.Warning(
                                $"{folder} had been deleted in {sw.ElapsedMilliseconds}ms"
                            );
                        }
                    }
                    Thread.Sleep(TimeSpan.FromHours(1));//check per hours
                }
            });