serilog / serilog-aspnetcore

Serilog integration for ASP.NET Core

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

No0Vad opened this issue · comments

commented

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

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

commented

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!