serilog / serilog-aspnetcore

Serilog integration for ASP.NET Core
Apache License 2.0
1.32k stars 209 forks source link

CreateBootstrapLogger does not work with configuration from file #381

Closed nandor23 closed 1 month ago

nandor23 commented 1 month ago

Hello!

Description I am trying to log before the app is running, by using the CreateBootstrapLogger, but it looks like the configurations are not applied from appsettings.json.

There are 2 sinks provided: Serilog.Sinks.Console and Serilog.Sinks.Seq, but the logs are nowhere to be found. The .ReadFrom.Configuration() has no effect at all, and the configuration is not null. Using .WriteTo.Console(), or .WriteTo.Seq() takes effect, so there must be something going on with the .ReadFrom.Configuration() method.

Bootstrap logger:

Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(builder.Configuration.GetSection("Serilog"))
    .CreateBootstrapLogger();

Log.Information("Test information");    // this isn't logged

After build, the logs work as expected, and configuration from appsettings.json is applied. This is the default Serilog setup:

builder.Host.UseSerilog((context, loggerConfig) =>
{
    loggerConfig.ReadFrom.Configuration(context.Configuration);
});

Just to have erything here, the following is the configuration:

"Serilog": {
  "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.Seq" ],
  "MinimumLevel": {
    "Default": "Information",
    "Override": {
      "Microsoft": "Information",
      "System": "Information"
    }
  },
  "WriteTo": [
    { "Name": "Console" },
    {
      "Name": "Seq",
      "Args": { "serverUrl": "http://localhost:5883" }
    }
  ],
  "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
  "Properties": {
    "Application": "LinkService"
  }
}

Expected behavior Asp.Net logs should work with Bootstrap logger

Relevant package, tooling and runtime versions Serilog.AspNetCore: 8.0.2 Serilog.Sinks.Seq: 8.0.0

nblumhardt commented 1 month ago

Hi! builder.Configuration.GetSection("Serilog") seems like it should be just builder.Configuration - ?

nandor23 commented 1 month ago

Thank you! It is working that way.