0
votes

I am pretty new to .Net Core. And I'm working on basic logging of information by the default ILogger provider. Initially my AppSetting.json was uncommented and able to see the logs that I've written.

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "AllowedHosts": "*"
} 

Later, I commented theLogLevel properties and ran the application on Krestel server. Then I can still see the logged information in the console.

AppSettings.json

{
  "Logging": {
    //"LogLevel": {
    //  "Default": "Debug",
    //  "System": "Information",
    //  "Microsoft": "Information"
    //}
  },
  "AllowedHosts": "*"
}

Program.cs

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .ConfigureLogging((context, logging)
                    =>
                {
                    logging.ClearProviders();
                    logging.AddConfiguration(context.Configuration.GetSection("Logging"));
                    logging.AddConsole();
                })
                .UseStartup<Startup>();

Sample logging method in HomeController.cs

public void LogExceptionToConsole()
        {
            _logger.LogError("This is raised by logger information");
        }

Console

enter image description here

Why is this behavior? If this the behavior why it has been designed like this? Could anyone explain me.

Thanks in advance

1
did you check the settings in appsettings.development.json? - Bob Ash
LogLevel just set what kind of log you want to see.Commenting it does not mean disable the logging.If you do not want to see error , try to set all log level to Critical in appsettings.development.json - Ryan

1 Answers

0
votes

ASP.NET Core defines the following log levels, ordered here from lowest to highest severity:

Trace = 0 (Disabled by default)
Debug = 1 
Information = 2
Warning = 3
Error = 4
Critical = 5

When no LogLevel is set in the application (either in startup.cs or program.cs or appsettings.json or appsettings.development.json) for a LogCategory, then Minimum LogLevel is set as Information by default.

Hence, in your example, the Error is logged since it is higher than the default Minimum LogLevel.

You can refer to this link for more details