Filtering log messages

Different execution modes of your application can require different levels of logging. For example, in development mode, you might want to see log events from all application areas, no matter what level they are from. While in production, you probably want to see only log events with warnings and errors that are produced by your application logic, and not from the ASP.NET infrastructure. Luckily, the ASP.NET logging framework makes it very easy to configure a filter with the application settings file.

To set the minimum level of the log messages you allow, open the appsettings.json file and add the Logging element, like this:

{
...
"Logging": {
"LogLevel": {
"Default": "Warning"
}
}
}

In the preceding example, we set the minimum allowed level to Warning, which means that only events with a level of Warning, Error, and Fatal will be allowed to be written. 

You can also set different filter rules for different categories. For example, you may wish to see all logs that you produce in your application logic, but filter out any log event that comes from the ASP.NET Core pipeline, that is, logs that originate from classes that are part of the Microsoft.AspNetCore namespace. Because each logger category is defined to be the class's full name to which the logger belongs, this task is easily accomplished like this:

{
...
"Logging": {
"LogLevel": {
"Default": "Debug",
"Microsoft.AspNetCore": "Warning"

}
}
}

In the preceding example, I set a specific rule for logs in the Microsoft.AspNetCore category, so only log events with a Warning level and above will allowed to be written, while other events will be written if they have a level of at least Debug.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.110.183