在Appsettings.json日志记录上下文中MinimumLevel和Override是什么意思? [英] What does MinimumLevel and Override mean in appsettings.json logging context?
问题描述
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
.
推荐答案
默认情况下,您说的是严重性为"Debug"的任何日志条目.或更高级别的邮件应发送到您的接收器(控制台,文件等).
By default, you're saying any log entry that is severity "Debug" or higher should be sent to your sink(s) (console, file, etc).
这类似于 Microsoft的日志记录文件:
例如,日志记录配置通常由应用程序设置文件的
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
级别./p>
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();
他们对该过程的解释是:
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 :>
Another explanation, from 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中的类型所有时,以上配置的作用是仅在
命名空间.警告
级别或更高级别生成事件.*
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屋!