c# - 在 appsettings.json 日志记录上下文中,MinimumLevel 和 Override 意味着什么?

标签 c# .net logging .net-core

我正在查看 Serilog sample project 中的 appsettings.json ,其中包含以下代码片段:

"MinimumLevel": {
  "Default": "Debug",
  "Override": {
    "System": "Information",
    "Microsoft": "Information"
  }
}

在这种情况下,Override 的目的是什么? SystemMicrosoft 条目在 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 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.

覆盖部分用于更改给定 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 for Microsoft namespace to Information. 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 a Microsoft.* namespace.

关于c# - 在 appsettings.json 日志记录上下文中,MinimumLevel 和 Override 意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54990647/

相关文章:

c# - 应该多久关闭/打开一次连接?

.net - 如何设置日期从和到月份的开始和结束日期?

java - 关于 log4j.xml NullAppender

logging - JBoss AS 6 日志模式

c# - 使用触发器更改数据库中的记录

c# - 无法在 web api Controller 中命中断点

c# - CheckedListBox内存泄漏

.net - 使用反射查找所有公共(public)虚拟方法并提供覆盖

c# - 如何以引用为标准进行 NHibernate 查询?

java - 日志记录不显示