问题
I am looking at the appsettings.json from Serilog sample project, which has the following snippet:
"MinimumLevel": {
"Default": "Debug",
"Override": {
"System": "Information",
"Microsoft": "Information"
}
}
In this context, what is the purpose of Override? The System and Microsoft entries don't have a parent setting in the MinimumLevel node, so what is it overriding?
Unless I am completely misunderstanding the purpose of Override.
回答1:
By default, you're saying any log entry that is severity "Debug" or higher should be sent to your sink(s) (console, file, etc).
This is similar behavior to Microsoft's document on logging:
For example, logging configuration is commonly provided by the
Loggingsection of app settings files. The following example shows the contents of a typical appsettings.Development.json file:{ "Logging": { "LogLevel": { "Default": "Debug", "System": "Information", "Microsoft": "Information" }, "Console": { "IncludeScopes": true } } }[...]
The
LogLevelproperty underLoggingspecifies the minimum level to log for selected categories. In the example,SystemandMicrosoftcategories log atInformationlevel, and all others log atDebuglevel.
The override sections are for changing log entries that have those namespaces. Normally you'd want to see Debug log entries for your code, but a higher level (like Information or Warning) for "not your code", like Microsoft and System.
Instead of putting this configuration in a configuation file you could also use code:
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseSerilog((ctx, cfg) =>
{
cfg.ReadFrom.Configuration(ctx.Configuration)
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information);
})
.Build();
Code from kimsereyblog.blogspot.com
Their explanation for the process is:
From the example we saw that we can configure a default minimum level of logging
.MinimumLevel.Debug(). We can also override that default for certain namespaces, for example here we set the minimum level of logging forMicrosoftnamespace toInformation. This will prevent ASP.NET Core to log all debug logs while keeping our own debug logs.
Another explanation, from nblumhardt.com:
The first argument of
Overrideis a source context prefix, which is normally matched against the namespace-qualified type name of the class associated with the logger....
The effect of the configuration above, then, is to generate events only at or above the
Warninglevel when the logger is owned by a type in aMicrosoft.*namespace.
来源:https://stackoverflow.com/questions/54990647/what-does-minimumlevel-and-override-mean-in-appsettings-json-logging-context