在 appsettings.json 日志上下文中,MinimumLevel 和 Override 是什么意思? [英] What does MinimumLevel and Override mean in appsettings.json logging context?
问题描述
我正在查看 Serilog 示例项目,其中包含以下代码段:
I am looking at the appsettings.json from Serilog sample project, which has the following snippet:
"MinimumLevel": {
"Default": "Debug",
"Override": {
"System": "Information",
"Microsoft": "Information"
}
}
在这种情况下,Override
的目的是什么?System
和 Microsoft
条目在 MinimumLevel
节点中没有父设置,那么它覆盖了什么?
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?
除非我完全误解了Override
的目的.
Unless I am completely misunderstanding the purpose of Override
.
推荐答案
默认情况下,您说的是任何严重性为调试"的日志条目.或更高版本应发送到您的接收器(控制台、文件等).
By default, you're saying any log entry that is severity "Debug" or higher should be sent to your sink(s) (console, file, etc).
这类似于 微软关于登录的文档:
例如,日志配置通常由应用设置文件的 Logging
部分提供.以下示例显示了典型 appsettings.Development.json 文件的内容:
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
}
}
}
[...]
Logging
下的 LogLevel
属性指定所选类别的最低记录级别.在示例中,System
和 Microsoft
类别在 Information
级别记录,所有其他类别在 Debug
级别记录.
The LogLevel
property under Logging
specifies the minimum level to log for selected categories. In the example, System
and Microsoft
categories log at Information
level, and all others log at Debug
level.
覆盖部分用于更改具有这些命名空间的日志条目.通常,您希望看到 您的代码 的 Debug
日志条目,但是更高级别(如 Information
或 Warning
>) 表示不是您的代码",例如 Microsoft 和 System.
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();
来自 kimsereyblog 的代码.blogspot.com
他们对这个过程的解释是:
Their explanation for the process is:
从示例中我们看到,我们可以配置默认的最低日志记录级别 .MinimumLevel.Debug()
.我们还可以覆盖某些命名空间的默认值,例如在这里我们将 Microsoft
命名空间的最低日志记录级别设置为 Information
.这将防止 ASP.NET Core 在保留我们自己的调试日志的同时记录所有调试日志.
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:
Override
的第一个参数是源上下文前缀,它通常与与记录器关联的类的命名空间限定类型名称匹配.
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.
...
那么,当记录器由 Microsoft.*
中的类型拥有时,上述配置的效果是仅生成处于或高于 Warning
级别的事件命名空间.
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 a Microsoft.*
namespace.
这篇关于在 appsettings.json 日志上下文中,MinimumLevel 和 Override 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!