我正在查看 Serilog sample project 中的 appsettings.json ,其中包含以下代码片段:
"MinimumLevel": {
"Default": "Debug",
"Override": {
"System": "Information",
"Microsoft": "Information"
}
}
在这种情况下,Override
的目的是什么? System
和 Microsoft
条目在 MinimumLevel
节点中没有父级设置,那么它覆盖的是什么?
除非我完全误解了Override
的目的。
最佳答案
默认情况下,您所说的任何严重性为“调试”或更高级别的日志条目都应发送到您的接收器(控制台、文件等)。
这与 Microsoft's document on logging 类似。 :
For example, logging configuration is commonly provided by the
Logging
section 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
LogLevel
property underLogging
specifies the minimum level to log for selected categories. In the example,System
andMicrosoft
categories log atInformation
level, and all others log atDebug
level.
覆盖部分用于更改给定 namespace 类型的最低日志级别。在您的示例中,这意味着来自 System.*
或 Microsoft.*
命名空间的日志必须是信息或更高级别才能显示。
通常情况下,您希望查看代码的调试
日志条目,但是更高级别的日志条目(例如信息
或警告
)表示“不是您的代码”,例如 Microsoft 和 System。
除了将此配置放入配置文件中,您还可以使用代码:
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
他们对该过程的解释是:
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 forMicrosoft
namespace toInformation
. This will prevent ASP.NET Core to log all debug logs while keeping our own debug logs.
另一个解释,来自nblumhardt.com :
The first argument of
Override
is 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
Warning
level when the logger is owned by a type in aMicrosoft.*
namespace.
关于c# - 在 appsettings.json 日志记录上下文中,MinimumLevel 和 Override 意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54990647/