serilog / serilog-settings-configuration

A Serilog configuration provider that reads from Microsoft.Extensions.Configuration
Apache License 2.0
455 stars 129 forks source link

Allow an array-style config format for Serilog:MinimumLevel:Override #214

Closed jez9999 closed 4 years ago

jez9999 commented 4 years ago

Starting afresh because the other issue was closed.

At the moment, the way you specify Serilog's minimum level overrides in configuration is in the following format:

"MinimumLevel": {
    "Default": "Information",
    "Override": {
        "Microsoft.AspNetCore.Mvc.Infrastructure": "Debug",
        "Microsoft": "Warning",
        "Microsoft.Hosting.Lifetime": "Information",
        "Microsoft.AspNetCore.Authentication": "Verbose",
    }
}

The source context (or 'namespace') is specified in the setting name, rather than a value. The trouble with this is that not all configuration providers lend themselves to having long, complex configuration key names. For example, I've tried to configure the equivalent to this on an Azure web app (using the double-underscore namespace separator as a replacement for the colon), and the best I could get to was:

"Serilog:MinimumLevel:Override:Microsoft_AspNetCore_Mvc_Infras": "Debug"

On my local machine, which is using appsettings.json, this is how it (correctly) looks:

"Serilog:MinimumLevel:Override:Microsoft.AspNetCore.Mvc.Infrastructure": "Debug"

The dots have been replaced with underscores, and more importantly there is a very restrictive length limit on config key names from these environment variables. So I think the solution is not to try and put complex/long information like a namespace in the key name, but as a key value. Thus, I propose that Serilog support the following syntax for minimum level overrides:

"MinimumLevel": {
    "Default": "Information",
    "Override": [
        {
            "SourceContext": "Microsoft.AspNetCore.Mvc.Infrastructure",
            "Level": "Debug"
        },
        {
            "SourceContext": "Microsoft",
            "Level": "Warning"
        },
        {
            "SourceContext": "Microsoft.Hosting.Lifetime",
            "Level": "Information"
        },
        {
            "SourceContext": "Microsoft.AspNetCore.Authentication",
            "Level": "Verbose"
        }
    ]
}

When Override is an array, it takes a set of objects with SourceContext/Level that specify the override level for the given source context. This results in the following kind of config structure, which works even with config providers that have restrictive key names:

"Serilog:MinimumLevel:Override:0:SourceContext": "Microsoft.AspNetCore.Mvc.Infrastructure"
"Serilog:MinimumLevel:Override:0:Level": "Debug"
skomis-mm commented 4 years ago

@jez9999

So where did you hit "61 characters limit" on key names? Can you make a repro steps? Or is having "dots" in namespace name is the only reason for a new syntax?

jez9999 commented 4 years ago

Looks like it's actually 64 characters. To repro:

  1. Create an Azure web app capable of outputting its key names and settings.
  2. Create setting with key name x234567890x234567890x234567890x234567890x234567890x234567890x234567890xxxxx
  3. Run web app
  4. See that the app gets the setting with key name x234567890x234567890x234567890x234567890x234567890x234567890x234
sungam3r commented 4 years ago

Fixed by #212

skomis-mm commented 4 years ago

Thanks. So it is Linux Web App running-in-container limitation. Need think about it. Here the link for a reference: https://techcommunity.microsoft.com/t5/azure-app-service/things-you-should-know-web-apps-and-linux/ba-p/392472#EnvLimit

skomis-mm commented 4 years ago

Currently, if App Setting names are longer than 64 characters, only the first 64 characters of the environment variable will be imported into the container.

The key word Currently. It is a matter of time when this limitation will be cleared. So some workarounds should be enough. Take this one-liner:

public static class Extensions
{
    public static LoggerConfiguration ApplyCustomOverrides(this LoggerConfiguration cfg, IConfigurationSection section)
        => section.GetChildren().Aggregate(cfg, (c, s) => c.MinimumLevel.Override(s.GetValue<string>("SourceContext"), s.GetValue<LogEventLevel>("Level")));
}

Config:

"MinimumLevel": {
    "Default": "Information",
    "OverrideList": [
        {
            "SourceContext": "Microsoft.AspNetCore.Mvc.Infrastructure",
            "Level": "Debug"
        },
        {
            "SourceContext": "Microsoft",
            "Level": "Warning"
        },
        {
            "SourceContext": "Microsoft.Hosting.Lifetime",
            "Level": "Information"
        },
        {
            "SourceContext": "Microsoft.AspNetCore.Authentication",
            "Level": "Verbose"
        }
    ]
}

Usage:

var logger = new LoggerConfiguration()
    .ApplyCustomOverrides(configuration.GetSection("Serilog:MinimumLevel:OverrideList"))
    .ReadFrom.Configuration(configuration)
    .CreateLogger();
jez9999 commented 4 years ago

How do you know it's a matter of time?