cocowalla / serilog-sinks-file-header

Plugin for the Serilog File sink that writes a header at the start of each log file
Apache License 2.0
10 stars 3 forks source link

Header repeated every 30 mins when Serilog configured to rollOnFileSizeLimit #3

Open pmerwood opened 4 years ago

pmerwood commented 4 years ago

A Header is being incorrectly added to a log file at exactly 30 min intervals when Serilog is configured to roll a log file when reaching a file size limit. Our Serilog initialisation code is shown below:

// Assemble the file header that's written when the log is created and repeated at the top of each log file. header.AppendLine(new string('*', 120)); header.AppendLine(string.Format("Application started at {0} {1}", startTime.ToShortDateString(), startTime.ToShortTimeString())); header.AppendLine(String.Format("Executable: {0}. Version {1}.{2} Build {3}.", System.Reflection.Assembly.GetExecutingAssembly().Location, System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).FileMajorPart, System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).FileMinorPart, System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).FileBuildPart)); header.AppendFormat("Environment: Host = '{0}'; Operating System = '{1}', Region = '{2}'; Installed UI Culture = '{3}'.", Environment.MachineName, Environment.OSVersion, culture.EnglishName, culture.Name);
Log.Logger = new LoggerConfiguration()
    .Enrich.FromLogContext()
    .Enrich.WithThreadId()
    .Enrich.WithThreadName()

    .MinimumLevel.ControlledBy(levelSwitch)

    .WriteTo.File(FileName,
        buffered: false,
        fileSizeLimitBytes: Size * 1048576,  // Convert to Bytes
        retainedFileCountLimit: Retain,
        rollOnFileSizeLimit: true,
        outputTemplate: "{Timestamp:dd/MM/yyyy HH:mm:ss.fff} {Level:u3} [{ThreadId} {ThreadName}] {Message:lj}{NewLine}{Exception}",
        hooks: new HeaderWriter(header.ToString,true))

 .CreateLogger();
cocowalla commented 4 years ago

I've been able to replicate this. As I thought, it's an issue in the Serilog File Sink, rather than this extension - I'll go ahead and open an issue in that repo, linking it back to here.

pmerwood commented 4 years ago

Thanks for following up. I’ve had a look at the issue you’ve logged for the serilog-file-sink and will watch that with interest.