Home > Blockchain >  Serilog does not write application initial logs to console with appsettings
Serilog does not write application initial logs to console with appsettings

Time:09-14

I have configured appsettings.json file to write console log. The application uses dotnet core 6.

{
  "Serilog": {
    "Using": [ "Serilog.Sinks.Console" ],
    "MinimumLevel": "Debug",
    "WriteTo": [
      { "Name": "Console" }
    ]
  }
}

And read from configuration like following.

builder.Logging.ClearProviders();

var logger = new LoggerConfiguration()
    .ReadFrom.Configuration(builder.Configuration)
    .CreateLogger();

builder.Logging.AddSerilog(logger);

But when I run the application, inital logs does not write to console. I have a black empty console.

CodePudding user response:

my MainAsync is like this, in a .net6.0 console app

Log.Logger = new LoggerConfiguration()
  .ReadFrom.Configuration(Configuration)
  .CreateLogger();

From memory you need to tell Serilog the configuration. Notice the Log.Logger I have never tried to plumb in Serilog into the builder.logger config like you have.

{
  "Serilog": {
    "Using": [ "Serilog.Sinks.Console" ],
    "MinimumLevel": "Verbose",
    "WriteTo": [
      { "Name": "Console" },
      {
        "Name": "File",
        "Args": {
          "path": "D:\\_tmp\\LM3.NNB.Organisation.Deduping.txt",
          "rollingInterval": "Day"
        }
      }
    ],
    "Properties": {
      "Application": "LM3.NNB.Organisation.Deduping"
    }
  }
}

CodePudding user response:

Check the documentation, and try to use Log.Logger as pointed by @Rippo.


Can you try configuring it through the Host in Program.cs ?

(.net core 6, adapt it if you have a different version)

builder.Host.UseSerilog((hostContext, services, configuration) => {
    configuration.MinimumLevel.Is(LogEventLevel.Debug)
    .Enrich.FromLogContext()
    .WriteTo.Console()

    .WriteTo.Debug() // also try writing to the debug console and see if it output something
});
  • Related