1

I am using Serilog to create log files in C#/.NET. Now there is a task to create a new instance of the log file if fileSizeLimitBites is full and the request is to limit it to 5 log files, if the fifth file is full there is no need to create a sixth, seventh... I've tried with "retainedFileCountLimit": 5 but with this deletes all old files from the folder where the logs are stored. I don't want to delete the old files, I want to keep the old files in the folder and not touch them and create 5 new instances of the log files. Does anyone know how to do this? Is that possible? Create 5 log instances and keep the old files?

  "Serilog": {
    "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Sinks.Seq" ],
    "Default": "Debug",
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Serilog.AspNetCore.RequestLoggingMiddleware": "Information",
        "Microsoft": "Warning",
        "Microsoft.EntityFrameworkCore": "Warning",
        "System": "Warning",
        "Microsoft.AspNetCore.Authentication": "Information"
      }
    },
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "..\\logs\\log-rfzo-test-.log",
          "rollingInterval": "Day",
          "fileSizeLimitBytes": "10240",
          "rollOnFileSizeLimit": true,
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] {Message}{NewLine}{Exception}"
        }
      }

I have tried with retainedFileCountLimit: 5, retainedFileCountLimit: 5 and 'null'

  • "Create 5 log instances and keep the old files?" seems confusing to me. You either are saying "keep me max 5" or you re not asking for that. Can you put it in different terms? what are "log instances"? Ultimately one file is written at a time, and yo are defining some limits for what to do when it says "oh look we need to add a file - lets do tidying per the requested limits" – Ruben Bartelink Jun 20 '23 at 23:16
  • @RubenBartelink on location "path": "..\\logs\\ there are logs from yesterday, day before yesterday etc. Let's say that I'm working with application today and log for today is created on same location as logs from yesterday and other days. Logs from today has fileSizeLimitBytes full and I want to create new log with extension _01 and do that till _05 is created, and after that if log with extension _05 has fileSizeLimitBytes full I don't want to create any file, but I want to keep old files from yesterday etc. I 've tried with retainedFileCountLimit:5 but with that approach I delete old files. – DraganMilovac1993 Jun 21 '23 at 08:37
  • Ah, that's clearer (I think). I'm pretty sure that the impl in Serilog.Sinks.File a) is b) is intended to stay pretty rudimentary, i.e. a flat set of files with a max count. The criteria of when to roll (size, end of day) decides when a new file is created and/or writing to an existing one stops. When a new file is being started, the trim happens, based on that criteria. Best way to understand it is to read the tests (clearly I have not read it in some years!). What you're asking for feels like it's far beyond what the sink does OOTB. – Ruben Bartelink Jun 21 '23 at 15:27
  • But, all is not lost; IIRC there is a file lifecycle hooks mechanism which will likely let you plug in what you want. If you're at the point where that does e.g. 95% and need a feature added, then it might make sense to add such a feature; for now, I'd stick with trying to figure out if that approach works (I personally have never gone down this road - complex trimming and/or reading files on disk on a server is nothing but bad news in my book) – Ruben Bartelink Jun 21 '23 at 15:29
  • Its a bit strange that you WANT to lose logs after reaching a certain number. Usually you want to lose oldest logs, not the newest. I don't think Serilog creators implemented anything specifically for that. – Tanuki Jun 21 '23 at 23:28

0 Answers0