BedeGaming / sinks-rollingfile

A serilog sink for rolling files based on size and time
Apache License 2.0
15 stars 21 forks source link

RollingFile logger doesn't append logs for small fileSizeLimitBytes and flushToDiskInterval #31

Closed rpiesnikowski closed 7 years ago

rpiesnikowski commented 7 years ago

Hi, Basically I was wondering whether I can make RollingFile similar to logrotate in that manner that after file exceed n bytes new file will be created. So I started testing with very small values and saw that rollingFile only store first n bytes and skip next log information. I'm using .net core 1.1 under windows 7. I used this values to test RollingFile:

If I'm right and this is a bug, so this also mean that after reach 1GB of log each day, no more logs will be collected in RollingFile??

To compare I've two sinks File - this is configured in json (as final solution), and RollingFile (in code for test)

 "Serilog": {
    "Using": [ "Serilog.Sinks.Literate", "Serilog.Sinks.RollingFile" ],
    "MinimumLevel": {
      "Default": "Verbose",
      "Override": {
        "OAuth.Services.Behaviours.SoapClientMessageInspector": "Verbose"
      }
    },
    "WriteTo": [
      {
        "Name": "LiterateConsole",
        "Args": {
          "outputTemplate": "[{Timestamp:HH:mm:ss} {SourceContext} [{Level}] {Message}{NewLine}{Exception}"
        }
      },
      {
        "Name": "File",
        "Args": {
          "path": "C:\\temp\\OAuthSTS.log",
          "outputTemplate": "{Timestamp:o} [{Level:u3}] ({SourceContext}/{MachineName}/{ThreadId}) {Message}{NewLine}{Exception}"
        }
      },
      //{
      //  "Name": "RollingFile",
      //  "Args": {

      //    "pathFormat": "%TEMP%\\Logs\\OAuthSTS-{Date}.log",
      //    "outputTemplate": "{Timestamp:o} [{Level:u3}] ({SourceContext}/{MachineName}/{ThreadId}) {Message}{NewLine}{Exception}",
      //    "fileSizeLimitBytes": "2048"

      //  }
      //}
    ],
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ]
  }

And for RollingFile:

 Log.Logger = new LoggerConfiguration()
                .ReadFrom.Configuration(Configuration)
                .WriteTo.RollingFile(@"C:\temp\OAuthSTS.log", Serilog.Events.LogEventLevel.Verbose, fileSizeLimitBytes: 2048
                , flushToDiskInterval: TimeSpan.FromSeconds(10)
                , outputTemplate: "{Timestamp:o} [{Level:u3}] ({SourceContext}/{MachineName}/{ThreadId}) {Message}{NewLine}{Exception}"
                ).CreateLogger();

Also I've put dispose: true

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddSerilog(dispose: true);
            app.UseDeveloperExceptionPage();
            app.UseIdentityServer();
        }

Finally output directory is:

PS C:\temp> dir
    Directory: C:\temp
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2017-01-26     08:09          14848 OAuthSTS.log
-a----       2017-01-26     08:09           2152 OAuthSTS-20170126.log
PS C:\temp>
AndyWardle commented 7 years ago

Hi @rpiesnikowski ,

From your examples you seem to be using the default Serilog RollingFile sink:

.WriteTo.RollingFile(@"C:\temp\OAuthSTS.log", Serilog.Events.LogEventLevel.Verbose, fileSizeLimitBytes: 2048

This sink is actually created using .WriteTo.RollingFileAlternate, you can see the example in the readme here: https://github.com/BedeGaming/sinks-rollingfile#file-size-limit. We've used this in production for over a year now with log files rolling over on size fine.

rpiesnikowski commented 7 years ago

Hi @AndyWardle Indeed you was right. Change to RollingFileAlternate solve my problems. Thanks for helping