简体   繁体   中英

Serilog does not write application initial logs to console with appsettings

I have configured appsettings.json file to write console log. The application uses do.net 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.

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
});

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"
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM