Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

ASP.NET Core LogLevel configuration

Suggested Videos
Part 61 - Logging in ASP.NET Core | Text | Slides
Part 62 - Logging exceptions in ASP.NET Core | Text | Slides
Part 63 - Logging to file in asp.net core using nlog | Text | Slides

In this video we will discuss the significance of LogLevel configuration in ASP.NET Core.

LogLevel indicates the severity of the logged message. It can be any of the following. They are listed here from lowest to highest severity.
  • Trace = 0
  • Debug = 1
  • Information = 2
  • Warning = 3
  • Error = 4
  • Critical = 5
  • None = 6

LogLevel Enum

LogLevel enum is present in Microsoft.Extensions.Logging namespace


namespace Microsoft.Extensions.Logging
{
    public enum LogLevel
    {
        Trace = 0,
        Debug = 1,
        Information = 2,
        Warning = 3,
        Error = 4,
        Critical = 5,
        None = 6
    }
}

LogLevel in appsettings.json 

LogLevel setting in appsettings.json file is used to control how much log data is logged or displayed. 

{
  "Logging": {
    "LogLevel": {
      "Default": "Trace",
      "Microsoft": "Warning"
    }
  }
}

ILogger Methods

On the ILogger interface, we have log methods that include the log level in the method name. For example to log a TRACE message we use LogTrace() method. Similarly to log a WARNING message we use LogWarning() method. Notice, except for LogLevel = None, we have a corresponding method for every log level.

LogTrace()
LogDebug()
LogInformation()
LogWarning()
LogError()
LogCritical()

LogLevel Example

Consider the following Details() action in HomeController

public class HomeController : Controller
{
    public ViewResult Details(int? id)
    {
        logger.LogTrace("Trace Log");
        logger.LogDebug("Debug Log");
        logger.LogInformation("Information Log");
        logger.LogWarning("Warning Log");
        logger.LogError("Error Log");
        logger.LogCritical("Critical Log");

        // Rest of the code
    }
}

The following is the LogLevl configuration in appsettings.json file.

{
  "Logging": {
    "LogLevel": {
      "Default": "Trace",
      "Microsoft": "Warning"
    }
  }
}

The following Log output is displayed in the Debug Output window. Since we have set "Default": "Trace", we see everything from Trace level and higher. Since Trace is the lowest level we see all the logs.

EmployeeManagement.Controllers.HomeController:Trace: Trace Log
EmployeeManagement.Controllers.HomeController:Debug: Debug Log
EmployeeManagement.Controllers.HomeController:Information: Information Log
EmployeeManagement.Controllers.HomeController:Warning: Warning Log
EmployeeManagement.Controllers.HomeController:Error: Error Log
EmployeeManagement.Controllers.HomeController:Critical: Critical Log

However if you want WARNING and higher then set "Default": "Warning"

If you do not want anything logged set LogLevel to None. The integer value of LogLevel.None is 6, which is higher than all the other log levels. So nothing gets logged.

Log filtering in ASP.NET Core

Consider the following log statement

EmployeeManagement.Controllers.HomeController:Trace: My log message

EmployeeManagement.Controllers.HomeController is the LOG CATEGORY
Trace is the LOG LEVEL. Remeber log level can be (Trace, Debug, Information, etc...)

In simple terms, LOG CATEGORY is the fully qualified name of the class that logged the message. The log category is displayed as a string in the logged message so we can use it easily determine from which class the log came from. LOG CATEGORY is used to filter logs.

With the following LogLevel configuration, we see everything from Trace level and higher from the log category "EmployeeManagement.Controllers.HomeController". However, for the category "EmployeeManagement.Models.SQLEmployeeRepository" only Error level logs and higher are displayed.

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning",
      "EmployeeManagement.Controllers.HomeController": "Trace",
      "EmployeeManagement.Models.SQLEmployeeRepository": "Error",
      "Microsoft": "Warning"
    }
  }
}

The above LogLevel configuration applies to all logging providers. A logging provider is the component that stores or displays logs. For example, the Console logging provider displays logs on the console. Similarly, the Debug logging provider displays logs on the Debug window in Visual Studio.

Log Filtering by Log Category and by Logging Provider

It is also possible to filter logs by Provider and by Category. The following is an example. With the following configuration, for the Debug logging provider, Warning and higher level logs are logged and displayed for all the log categories. Where as for the rest of the logging providers, Trace and higher level logs are logged and displayed for all the log categories. 

{
  "Logging": {
    "Debug": {
      "LogLevel": {
        "Default": "Warning",
        "EmployeeManagement.Controllers.HomeController": "Warning",
        "EmployeeManagement.Models.SQLEmployeeRepository": "Warning",
        "Microsoft": "Warning"
      }
    },
    "LogLevel": {
      "Default": "Trace",
      "EmployeeManagement.Controllers.HomeController": "Trace",
      "EmployeeManagement.Models.SQLEmployeeRepository": "Trace",
      "Microsoft": "Trace"
    }
  }
}

LogLevel configuration in environment specific appsettings.json file

Please remember the configuration in environment specific appsettings.json file (for example appsettings.development.json) overrides the settings in appsettings.json file. Make sure the log level configuration in the environment specific appsettings.json file is what you really want, to avoid surprises.

asp.net core tutorial for beginners

No comments:

Post a Comment

It would be great if you can help share these free resources