Description
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"