Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the Default, System and Microsoft LogLevels in ASP.NET Core

The default project template has the following logging configuration in appSettings.json:

"Logging": {
  "IncludeScopes": true,
  "LogLevel": 
    "Default": "Debug",
    "System": "Information",
    "Microsoft": "Information"
  }
}

What are Default, System and Microsoft for?

like image 306
Muhammad Rehan Saeed Avatar asked May 24 '16 15:05

Muhammad Rehan Saeed


1 Answers

Both the System and Microsoft namespace assemblies have a logging level that is honored. Consider an MVC 6 application, imagine in your project.json that you have a dependency of "Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final" - this assembly is prefixed with "Microsoft". Internally its logging will be output with the level specified in your config.

Likewise, in your application "Default" is relevant to your application. Consider the following:

void FooBar(ILogger logger)
{
    logger.LogCritical("LoglLevel.Critical");
    logger.LogDebug("LoglLevel.Debug");
    logger.LogError("LoglLevel.Errror");
    logger.LogInformation("LoglLevel.Information");
    logger.LogTrace("LoglLevel.Trace"); // This message would not be written
    logger.LogWarning("LoglLevel.Warning");
}

// This is the severity
public enum LogLevel
{
    Trace,
    Debug,
    Information,
    Warning,
    Error,
    Critical,
    None
}

So if you set "Microsoft": "Critical" and internally MVC encounters and logs an exception via the logger.LogError method, it will not be written in the output of the log.

like image 51
David Pine Avatar answered Sep 30 '22 21:09

David Pine