serilog / serilog-aspnetcore

Serilog integration for ASP.NET Core
Apache License 2.0
1.29k stars 203 forks source link

Changing the Override in appsettings.json has no effect when app is running? #328

Closed No0Vad closed 1 year ago

No0Vad commented 1 year ago

Description Changing values to "Override" property in the appsettings.json during runtime has no effect.

Reproduction Sample appsettings.json

{
    "Serilog":
    {
        "MinimumLevel":
        {
            "Default": "Information",
            "Override":
            {
                "Microsoft": "Warning",
                "Microsoft.Hosting.Lifetime": "Information",
                "System.Net.Http.HttpClient": "Warning"
            }
        }
    },

    "AllowedHosts": "*"
}

Sample program.cs code bits

var builder = WebApplication.CreateBuilder(args);

builder.Host.UseSerilog((context, services, configuration) => configuration
    .ReadFrom.Configuration(context.Configuration)
    .Enrich.FromLogContext()
    .WriteTo.Console(outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u4}] ({SourceContext}->{Method}) {Message}{NewLine}{Exception}")
);

Now if I change the appsettings.json to this and save the file, nothing is changed. I expect a lot more the be seen in the log when I visit a controller.

{
    "Serilog":
    {
        "MinimumLevel":
        {
            "Default": "Information",
            "Override":
            {
                "Microsoft": "Verbose",
                "Microsoft.Hosting.Lifetime": "Verbose",
                "System.Net.Http.HttpClient": "Verbose"
            }
        }
    },

    "AllowedHosts": "*"
}

Only when I restart the app my changes take effect.

Expected behavior The overrides to change to the new values, and I should see a lot more log entries.

Relevant package, tooling and runtime versions .NET 7 Serilog.AspNetCore v6.1.0 Serilog.Sinks.Seq v5.2.2

Additional context Code samples are simple but it is used in a simple MVC app where ILogger is injected to the controller. Logs are also saved to Seq

augustoproiete commented 1 year ago

This is by design. Serilog prefers an immutable logging pipeline that the application uses for the remainder of its execution time.

In order for updates to the configuration to reflect in the app, you'll need to recreate the Serilog pipeline... Some alternatives would be:

N.B.: The best way to get answers on usage of Serilog is to post a question on stackoverflow.com including the serilog tag and details of the code you are using and some context the kind of app you are building

https://stackoverflow.com/questions/tagged/serilog

No0Vad commented 1 year ago

Ah, I assumed it was a bug since it did not do what I expected to do when it comes to the appsettings.json file.

Thanks for the links! I'll give them a look!

sungam3r commented 1 year ago

Also see https://github.com/serilog/serilog-settings-configuration/pull/352