Serilog - RollingFile Sink does not roll files based on date and size

只愿长相守 提交于 2019-12-12 11:24:42

问题


I am using Serilog - RollingFile Sink, but it stores all data in a single file for a day. In my application, 1 GB log is written in a day. So I want to roll log file on the basis of date and size.

How can I configure RollingFile Sink to roll files based on date and size?


回答1:


Nowadays Serilog.Sinks.RollingFile package is deprecated in favor of Serilog.Sinks.File (see the github project readme intro). Serilog.Sinks.File package has been upgraded to support file rolling. You can use the following Serilog config to enable rolling both by time and size:

"Serilog": {
    "Using": ["Serilog.Sinks.File"],
    "MinimumLevel": "Debug",
    "WriteTo": [
        {
            "Name": "File",
            "Args": {
                "path": "logs/log.txt",
                "rollingInterval": "Day",
                "rollOnFileSizeLimit": true,
                "fileSizeLimitBytes": "512",
                "retainedFileCountLimit": 3,
                "formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
            }
        }
    ]
  }

Then you will get something like this:




回答2:


I believe you're looking for this alternative implementation of the RollingFile sink:

Serilog Rolling File Sink (alternative)

This is a rolling file sink that allows you to specify roll over behaviour based on file size. https://github.com/BedeGaming/sinks-rollingfile




回答3:


From the documentation:

To avoid bringing down apps with runaway disk usage the rolling file sink limits file size to 1GB by default. The limit can be changed or removed using the fileSizeLimitBytes parameter.

.WriteTo.RollingFile("log-{Date}.txt", fileSizeLimitBytes: null)

The example shows removing the limit by setting it to null. In your case, set it to an appropriate size.

UPDATE

Yes, based on your comment I looked at the source code and it looks like the RollingFileSink's lowest unit of measure is a day so having more than one on the same day seems to be not supported. However, and I didn't look closely, it looks like the OpenFile methods in RollingFileSink.cs does something with sequence numbers. You might want to take a peek and see what that code is doing.




回答4:


The current version also implements rolling hourly. That might be another solution to your ploblem.

{Hour} Creates a file per hour. Filenames use the yyyyMMddHH format.

like: .WriteTo.RollingFile("log-{Date}.Hour")




回答5:


In appsettings.json you should write something like that:

{
"Serilog": {

  "Using": ["Serilog.Sinks.File"],
  "MinumumLevel": {
    "Default": "Error",
    "Override": {
      //"Microsoft": "Warning",
      //"System": "Warning",  
      "Microsoft.AspNetCore.Authentication": "Verbose",
      "WebApplicationLogger1.Startup": "Warning",
      "WebApplicationLogger1.Pages": "Warning"
    }
  },
  "WriteTo": [
    {
      "Name": "RollingFile",
      "Args": {
        "rollingInterval": "Day",  // --> THIS IS THAT YOU NEED
        "pathFormat": "C:\\Logfiles\\File-{Date}.log",
        "restrictedToMinimumLevel": "Warning"   
      }
     }
   ],
  }
}


来源:https://stackoverflow.com/questions/39821560/serilog-rollingfile-sink-does-not-roll-files-based-on-date-and-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!