Closed jez9999 closed 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?
Looks like it's actually 64 characters. To repro:
x234567890x234567890x234567890x234567890x234567890x234567890x234567890xxxxx
x234567890x234567890x234567890x234567890x234567890x234567890x234
Fixed by #212
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
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();
How do you know it's a matter of time?
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:
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:
On my local machine, which is using
appsettings.json
, this is how it (correctly) looks: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:
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: