ASP NET Core LogLevel configuration
Автор: kudvenkat
Загружено: 2019-05-31
Просмотров: 64054
In this video we will discuss the significance of LogLevel configuration in ASP.NET Core.
Text version of the video
https://csharp-video-tutorials.blogsp...
Healthy diet is very important for both body and mind. We want to inspire you to cook and eat healthy. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking.
/ @aarvikitchen5572
Slides
https://csharp-video-tutorials.blogsp...
ASP.NET Core Text Articles & Slides
https://csharp-video-tutorials.blogsp...
ASP.NET Core Tutorial
• ASP.NET core tutorial for beginners
Angular, JavaScript, jQuery, Dot Net & SQL Playlists
https://www.youtube.com/user/kudvenka...
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.
Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: