What does MinimumLevel and Override mean in appsettings.json logging context?

寵の児 提交于 2021-01-29 19:13:48

问题


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 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.

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 for Microsoft namespace to Information. 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 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.



来源:https://stackoverflow.com/questions/54990647/what-does-minimumlevel-and-override-mean-in-appsettings-json-logging-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!