serilog / serilog-sinks-file

Write Serilog events to files in text and JSON formats, optionally rolling on time or size
Apache License 2.0
334 stars 118 forks source link

Date-driven (rather than count-driven) file retention option #39

Closed stephenpatten closed 4 years ago

stephenpatten commented 6 years ago

Nick,

Aside from the fact that I (we) log too much, how do I keep all the log files created per day. It looks like we are only keeping the last 31 files. My thinking would be that with my config that the time period would trump the file size rolling behaviour. i.e. I should have 31 calendar days of logs and each day may have hundreds of files that rolled on size.. Hope that make some sense.

Below you'll find my logger bootstrap and the packages I'm using.

Thank you, Stephen

var filePath =
    $"{ConfigurationManager.AppSettings["LogFileBaseDir"]}\\ExternalFields_{Environment.MachineName}_.txt";

const string Workflow_Serilog_V6 = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}|{Level:u3}|{CorrelationId}|{MachineName}|{Application}|{ProcessId}|{ThreadId}|{SourceContext:l}|{Message}|{NewLine}{Exception}";

Log.Logger = new LoggerConfiguration()
    .ReadFrom.AppSettings()
    .Enrich.FromLogContext()
    .Enrich.WithMachineName()
    .Enrich.WithProcessId()
    .Enrich.WithThreadId()
    .Enrich.WithProperty("Application", "ExternalFields")
    /*https://github.com/serilog/serilog-sinks-file*/
    .WriteTo.Async(a => 
            a.File(filePath, 
            outputTemplate: Workflow_Serilog_V6, 
            rollingInterval: RollingInterval.Day, 
            rollOnFileSizeLimit: true, 
            fileSizeLimitBytes: 104857600, 
            buffered: true,
            flushToDiskInterval: TimeSpan.FromMilliseconds(500)))
    .CreateLogger();

image

stephenpatten commented 6 years ago

Looks like the packages got truncated

`

`

nblumhardt commented 6 years ago

@stephenpatten thanks for the note; this is by design at the moment (it's retainedFileCountLimit, not a duration), but also a decent extension to consider in the future πŸ‘

stephenpatten commented 6 years ago

Got it.

So no matter how large these are, (1GB by default) I will only retain 90 of them regardless of the date of creation.

Another quick question, do I have the file settings for buffering and flushing setup correctly to be used in an asynchronous operation?

  Log.Logger = new LoggerConfiguration()
            .ReadFrom.AppSettings()
            .Enrich.FromLogContext()
            .Enrich.WithMachineName()
            .Enrich.WithProcessId()
            .Enrich.WithThreadId()
            .Enrich.WithProperty("Application", "ExternalFields")
            /*https://github.com/serilog/serilog-sinks-file*/
            .WriteTo.Async(a => 
                    a.File(filePath, 
                    outputTemplate: workflowSerilogV6,
                    retainedFileCountLimit: 90,
                    rollingInterval: RollingInterval.Day, 
                    rollOnFileSizeLimit: true, 
                    buffered: true,
                    flushToDiskInterval: TimeSpan.FromMilliseconds(500)))
            .CreateLogger();
nblumhardt commented 6 years ago

Looks good πŸ‘

se7vanj commented 5 years ago

@stephenpatten thanks for the note; this is by design at the moment (it's retainedFileCountLimit, not a duration), but also a decent extension to consider in the future πŸ‘

@nblumhardt I tried AlternateRollingFile but often it writes 0KB, now I see SuperRollingFileAlternate available, but it will be helpful if we can see this feature on sinks-file itself.

thiagosgarcia commented 5 years ago

Just created PR #90 Worked for me. Hope this helps.

nblumhardt commented 4 years ago

In the current -dev package; thanks @thiagosgarcia πŸ‘