Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
stephenpatten opened this issue Nov 9, 2017 · 7 comments
Closed

Comments

@stephenpatten
Copy link

stephenpatten commented Nov 9, 2017

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
Copy link
Author

stephenpatten commented Nov 9, 2017

Looks like the packages got truncated

<packages> <package id="Serilog" version="2.5.0" targetFramework="net452" /> <package id="Serilog.Enrichers.Environment" version="2.1.2" targetFramework="net452" /> <package id="Serilog.Enrichers.Process" version="2.0.1" targetFramework="net452" /> <package id="Serilog.Enrichers.Thread" version="3.0.0" targetFramework="net452" /> <package id="Serilog.Formatting.Compact" version="1.0.0" targetFramework="net452" /> <package id="Serilog.Settings.AppSettings" version="2.1.2" targetFramework="net452" /> <package id="Serilog.Sinks.Async" version="1.1.0" targetFramework="net452" /> <package id="Serilog.Sinks.File" version="4.0.0" targetFramework="net452" /> <package id="Serilog.Sinks.RollingFile" version="3.3.0" targetFramework="net452" /> <package id="SerilogMetrics" version="2.0.3" targetFramework="net452" /> </packages>

@nblumhardt
Copy link
Member

@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
Copy link
Author

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
Copy link
Member

Looks good 👍

@nblumhardt nblumhardt changed the title How to change the limit of the file rotation? Date-driven (rather than count-driven) file retention option Nov 10, 2017
@se7vanj
Copy link

se7vanj commented Feb 1, 2019

@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
Copy link
Contributor

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

@nblumhardt
Copy link
Member

In the current -dev package; thanks @thiagosgarcia 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants