7

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?

mosakashaka
  • 535
  • 1
  • 6
  • 19

2 Answers2

8

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...

Ramil Aliyev 007
  • 4,437
  • 2
  • 31
  • 47
mosakashaka
  • 535
  • 1
  • 6
  • 19
-1

Be careful while typing. Previously you add a semicolon, and it don't compile this way:

loggerConfiguration
    .ReadFrom.Configuration(hostingContext.Configuration)**;**
    .WriteTo.Console();
Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69
  • 1
    This information doesn't answer the question and should rather be noted as a comment or omitted. The idea being that all "answers" are potentially an answer to the initial question, discussion and clarification occurs in the comments. If you have another StackExchange you can link them to get over the reputation requirement to make a comment. With that said, thank you for taking the time to assist :) – Tyler Szabo Oct 06 '20 at 21:29