Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serilog `rollOnFileSizeLimit` doesn't work with configuration file

When configure Serilog by configuration file(with nuget package Serilog.Settings.Configuration), it doesn't create rolling log file when size limit is reached.

As suggested in this question and this issue, I'm using Serlog.Sinks.File (version 4.0.0), but rolling file is not created.

This is my serilog config file appsettings.json:

{
  "Serilog": {
    "Using": [
      "Serilog.Sinks.File",
      "Serilog.Sinks.Console"
    ],
    "MinimumLevel": {
      "Default": "Debug",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning"
      }
    },
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "%LogPath%\\Logs\\log.txt",
          "rollOnFileSizeLimit ": true,
          "retainedFileCountLimit ": 20,
          "rollingInterval": "Day",
          "fileSizeLimitBytes": 10000
        }
      },
      {
        "Name": "Console"
      }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName" ],
    "Destructure": [
    ],
    "Properties": {
    }
  }
}

This is the code I tell Serilog to read from the configuration:

//previous code ommited... 
.ConfigureAppConfiguration((hostContext, configApp) =>
                {
                    Environment.SetEnvironmentVariable("LogPath", AppDomain.CurrentDomain.BaseDirectory);
                    configApp.AddJsonFile("appsettings.json", optional: false);

                    configApp.AddEnvironmentVariables();
                    configApp.AddCommandLine(args);
                })
                .UseSerilog((hostingContext, loggerConfiguration) =>
                {
                    loggerConfiguration
                        .ReadFrom.Configuration(hostingContext.Configuration);
                         .WriteTo.Console();
                })

When file size reaches 10KB, it stops growing, and no new log file is created. BTW, rolling by day is still working.

I also verified by configuring Serilog by code, and it works, so I think it's not related to the sink...

This is the code which works

 .UseSerilog((hostingContext, loggerConfiguration) =>
                {
                    loggerConfiguration.MinimumLevel.Debug()
                            .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
                            .Enrich.FromLogContext()
                            .WriteTo.File(path: Path.Combine(Environment.CurrentDirectory, "Logs", "log.txt"),
                                rollOnFileSizeLimit: true,
                                retainedFileCountLimit: 20,
                                rollingInterval: RollingInterval.Day,
                                fileSizeLimitBytes: 10000
                                )
                            .WriteTo.Console();
                })

So how could I make Serilog create rolling file when file size is reached by configuration file?

like image 227
mosakashaka Avatar asked Oct 16 '22 13:10

mosakashaka


1 Answers

I downloaded and debugged with the Serilog.Settings.Configuration source code, and found that I made a stupid mistake here. The no-effect parameter name in the config file has an extra space.

"rollOnFileSizeLimit ": true,
"retainedFileCountLimit ": 20,

should be

"rollOnFileSizeLimit": true,
"retainedFileCountLimit": 20,

After correcting this, all things work.

I assume maybe the project maybe should trim spaces before matching the keys...

like image 158
mosakashaka Avatar answered Nov 03 '22 21:11

mosakashaka