'.Net 5.0 ILogger method IsEnabled how is setting determined in "Logger" configuration?

Environment:

Windows 10
Microsoft Visual Studio Community 2019
Version 16.11.9
VisualStudio.16.Release/16.11.9+32106.194

Target framework is .NET 5.0

Trying to understand / learn Logging configuration features.

Logging configuration is picked up via the following statements:

host_builder.ConfigureLogging( configure_logging_callback );

... and the "configure_logging_callback" function contains these statements:

logging.ClearProviders();

logging.AddConsole();

logging.AddDebug();

logging.AddEventSourceLogger();

logging.AddEventLog();

logging.AddConfiguration( app_settings.config.GetSection( "Logging" ) );

I have a utility class with a "show_log_level" method, as shown below:

public static void show_log_level( ILogger logger )
{

     Type category_name = logger.GetType().GetInterfaces()[ 0 ].GetGenericArguments()[ 0 ];

     Console.WriteLine( "logger<" + category_name + ">:" );

     logger.LogTrace( "- LogTrace message" );
     logger.LogDebug( "- LogDebug message" );
     logger.LogInformation( "- LogInformation message" );
     logger.LogWarning( "- LogWarning message" );
     logger.LogError( "- LogError message" );
     logger.LogCritical( "- LogCritical message" );

     Console.WriteLine( $@"- LogLevel.Trace enabled: {logger.IsEnabled( LogLevel.Trace )}" );
     Console.WriteLine( $@"- LogLevel.Debug enabled: {logger.IsEnabled( LogLevel.Debug )}" );
     Console.WriteLine( $@"- LogLevel.Information enabled: {logger.IsEnabled( LogLevel.Information )}" );
     Console.WriteLine( $@"- LogLevel.Warning enabled: {logger.IsEnabled( LogLevel.Warning )}" );
     Console.WriteLine( $@"- LogLevel.Error enabled: {logger.IsEnabled( LogLevel.Error )}" );
     Console.WriteLine( $@"- LogLevel.Critical enabled: {logger.IsEnabled( LogLevel.Critical )}" );
     Console.WriteLine( $@"- LogLevel.None enabled: {logger.IsEnabled( LogLevel.None )}" );

}

I then perform the following tests:

TEST 1 - JSON:

   "Logging": {
      "LogLevel": {
         "Default": "Error"
      },
      "Console": {
         "IncludeScopes": true,
         "LogLevel": {
            "website1": "Error"
         }
      }
   }

TEST 1 - OUTPUT: Expected result - only Error and above is enabled

logger<website1.show_config>:
fail: website1.show_config[0]
      => ConnectionId:0HMF57AH5JJPF => RequestPath:/ RequestId:0HMF57AH5JJPF:00000001
      - LogError message
crit: website1.show_config[0]
      => ConnectionId:0HMF57AH5JJPF => RequestPath:/ RequestId:0HMF57AH5JJPF:00000001
      - LogCritical message
- LogLevel.Trace enabled: False
- LogLevel.Debug enabled: False
- LogLevel.Information enabled: False
- LogLevel.Warning enabled: False
- LogLevel.Error enabled: True
- LogLevel.Critical enabled: True
- LogLevel.None enabled: False

TEST 2 - JSON:

   "Logging": {
      "LogLevel": {
         "Default": "Information"
      },
      "Console": {
         "IncludeScopes": true,
         "LogLevel": {
            "website1": "Error"
         }
      }
   }

TEST 2 - OUTPUT: Why is LogLevel "Information" enabled? - as only "Error" and above is specified in "Console" provider

fail: website1.show_config[0]
      => ConnectionId:0HMF57B2RAQUB => RequestPath:/ RequestId:0HMF57B2RAQUB:00000001
      - LogError message
crit: website1.show_config[0]
      => ConnectionId:0HMF57B2RAQUB => RequestPath:/ RequestId:0HMF57B2RAQUB:00000001
      - LogCritical message
- LogLevel.Trace enabled: False
- LogLevel.Debug enabled: False
- LogLevel.Information enabled: True
- LogLevel.Warning enabled: True
- LogLevel.Error enabled: True
- LogLevel.Critical enabled: True
- LogLevel.None enabled: False

TEST 3 - JSON:

   "Logging": {
      "LogLevel": {
         "Default": "Information",
         "website1": "Error"
      },
      "Console": {
         "IncludeScopes": true,
         "LogLevel": {
            "website1": "Error"
         }
      }
   }

TEST 3 - OUTPUT: Adding "website1" category to "Logging:LogLevel" now gives expected result - only Error and above is enabled, but why do I have to specify it earlier in the configuration? Are minimum log levels retained when "Default" is present (no clue)?

fail: website1.show_config[0]
      => ConnectionId:0HMF57C76CSFT => RequestPath:/ RequestId:0HMF57C76CSFT:00000001
      - LogError message
crit: website1.show_config[0]
      => ConnectionId:0HMF57C76CSFT => RequestPath:/ RequestId:0HMF57C76CSFT:00000001
      - LogCritical message
- LogLevel.Trace enabled: False
- LogLevel.Debug enabled: False
- LogLevel.Information enabled: False
- LogLevel.Warning enabled: False
- LogLevel.Error enabled: True
- LogLevel.Critical enabled: True
- LogLevel.None enabled: False

Is there any official documentation that describes:

  • How the "Logging" configuration is processed (priorities, rules, etc.)?
  • How do the various log levels become enabled or disabled depending on the JSON properties and values that are present?
  • What are the specific JSON properties and values that can be present in the "Logging" configuration?

Thanks in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source