How to configure rolling serilog-sinks-file to keep the latest log file with the same name

906 views Asked by At

I like to have an editor open with the latest log file, while my application is running. I have a 10MB file size limit and Day as the rolling interval.

When the first file, say log-20210314.log fills up and serilog starts a new one, the new one has a new name log-20210314_001.log, so I need to change which file I'm looking at in my editor.

In contrast, log4j's RollingFileAppender default rollover strategy works like this:

Number of rollovers Active output target Archived log files Description
0 foo.log - All logging is going to the initial file.
1 foo.log foo-1.log During the first rollover foo.log is renamed to foo-1.log. A new foo.log file is created and starts being written to.
2 foo.log foo-2.log, foo-1.log During the second rollover foo.log is renamed to foo-2.log. A new foo.log file is created and starts being written to.
3 foo.log foo-3.log, foo-2.log, foo-1.log During the third rollover foo.log is renamed to foo-3.log. A new foo.log file is created and starts being written to.

In that example, if my editor is open to foo.log, I will always see the latest log entries (except for a few seconds when the file is empty immediately after a rollover).

How can I make serilog-sinks-file behave this way?

My config:

{
  "Serilog": {
    "MinimumLevel": "Debug",
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": ".\\logs\\log-.log",
          "rollingInterval": "Day",
          "rollOnFileSizeLimit": true,
          "fileSizeLimitBytes": 1000000,
          "restrictedToMinimumLevel": "Debug",
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}"
        }
      }
    ]
  }
}
0

There are 0 answers