Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order of settings - Which setting is read first? #451

Open
gthvidsten opened this issue Mar 2, 2025 · 1 comment
Open

Order of settings - Which setting is read first? #451

gthvidsten opened this issue Mar 2, 2025 · 1 comment

Comments

@gthvidsten
Copy link

Say you have the following configuration set up in code:

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Services.AddSerilog((services, config) =>
{
    config
        .ReadFrom.Services(services)
        .MinimumLevel.Override("MyAssembly", LogEventLevel.Verbose)
        .Enrich.FromLogContext()
        .WriteTo.Console()
        .WriteTo.File(
            Path.Combine(builder.Configuration["SomeCustomPathPath"] ?? string.Empty, "logs", "Application.log"), rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true)
        .ReadFrom.Configuration(builder.Configuration);
});

And my appsettings.json does not contain anything. I assume that the Serilog configuration will be as it is defined in code, or does .ReadFrom.Configuration() override the settings in code?

If I add the following to my appsettings.json:

{
  "Serilog": {
    "MinimumLevel": {
      "Override": {
        "MyAssembly": "Warning"
    }
}

Which log level will "MyAssembly" have now? Verbose or Warning?
Will this change If I place .ReadFrom.Configuration() above .MinimumLevel?

So basically, which order are the settings read in? Can you have i.e. settings for one sink in appsettings and another setting in code and they will both work, or neither, or only one of them?

@nblumhardt
Copy link
Member

Order is important for things like minimum levels/overrides: last-in wins.

For sinks and other objects added to the pipeline, configuration is read separately, so adding a file entry to the JSON configuration, and WriteTo.File() in code, will result in two instances of the file sink being configured.

(IMHO, which is not a popular one, the best way to configure Serilog and just about everything else is to do all configuration in code, only pulling key-value pairs from JSON config for things that actually vary at deployment time. Otherwise, I we're just switching a nwice type-safe, compile-time-validated language for a weaker one. YMMV :-))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants