Home > Mobile >  .Net 5.0 ILogger method IsEnabled how is setting determined in "Logger" configuration?
.Net 5.0 ILogger method IsEnabled how is setting determined in "Logger" configuration?

Time:02-03

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.

CodePudding user response:

Thanks to @Fei Han for pointing me to the updated .Net 6.0 documentation on log filtering which contains much more detail than the .Net 5.0 documentation I was referencing. Using the .Net 6 documentation, the behavior becomes much clearer, see annotated results below:

TEST 1 - JSON

   "Logging": {
      "LogLevel": {             // ALL providers, LogLevel applies to all the enabled providers.
         "Default": "Error"     // Default logging, Error and higher.
      },
      "Console": {              // These setting apply to the Console provider
         "IncludeScopes": true,
         "LogLevel": {
            "website1": "Error" // All website1* categories, Error and higher.
         }
      }
   }

TEST 1 - OUTPUT:

logger<website1.show_config>:       // Only Error and above will be logged for website1* categories
fail: website1.show_config[0]
      - LogError message
crit: website1.show_config[0]
      - LogCritical message

- LogLevel.Trace enabled: False
- LogLevel.Debug enabled: False
- LogLevel.Information enabled: False
- LogLevel.Warning enabled: False
- LogLevel.Error enabled: True      // Minimum level found at Logging:LogLevel:Default
- LogLevel.Critical enabled: True
- LogLevel.None enabled: False

TEST 2 - JSON:

   "Logging": {
      "LogLevel": {                 // ALL providers
         "Default": "Information"   // Default logging, Information and higher
      },
      "Console": {                  // These setting apply to the Console provider
         "IncludeScopes": true,
         "LogLevel": {
            "website1": "Error"     // Error and above will be logged
         }
      }
   }

TEST 2 - OUTPUT:

fail: website1.show_config[0]       // Only Error and above will be logged for website1* categories
      - LogError message
crit: website1.show_config[0]
      - LogCritical message

- LogLevel.Trace enabled: False
- LogLevel.Debug enabled: False
- LogLevel.Information enabled: True    // Minimum level found at Logging:LogLevel:Default
- LogLevel.Warning enabled: True
- LogLevel.Error enabled: True
- LogLevel.Critical enabled: True
- LogLevel.None enabled: False

TEST 3 - JSON:

   "Logging": {
      "LogLevel": {                 // ALL providers
         "Default": "Information",  // Default logging, Information and higher
         "website1": "Error"        // All website1* categories, Error and higher.
      },
      "Console": {                  // These setting apply to the Console provider
         "IncludeScopes": true,
         "LogLevel": {
            "website1": "Error"     // Error and above will be logged
         }
      }
   }

TEST 3 - OUTPUT:

fail: website1.show_config[0]       // Only Error and above will be logged for website1* categories
      - LogError message
crit: website1.show_config[0]
      - LogCritical message

- LogLevel.Trace enabled: False
- LogLevel.Debug enabled: False
- LogLevel.Information enabled: False
- LogLevel.Warning enabled: False
- LogLevel.Error enabled: True      // Minimum level found in Logging:Console:LogLevel:website1
- LogLevel.Critical enabled: True
- LogLevel.None enabled: False
  •  Tags:  
  • Related