Logging in ASP.NET Core (Part 1 - Configuration)
How to configure logging in ASP.NET Core
{
"DetailedErrors": true,
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
There are only two root keys. While DetailedErrors
might sound 'interesting', its not what we're looking for, since it only deals with Exceptions.
What we're looking for is the Logging
Section. It's a Dictionary of Category
and their logging level.
The Default
key is special, since it provides the default level for all unconfigured categories.
Categories
Everytime a Logger is created, a category is specified. That category is included with each log message created by that instance of the Logger. The category string is arbitrary, but the convention is to use the class name.
For example in our current Config the Category Microsoft.AspNetCore
affects the logging of the Asp.NET Framwork itself.
With the current configuration we have a Log level of Warning
from the AspNetCore
Framework and Information
for everything else.
Let's try it, start your Application using F5 and have a Look at the output in the CMD Window that opens.
The output in your CMD window should look similiar to this:
info: Microsoft.Hosting.Lifetime[14]
Now listening on: https://localhost:7252
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5252
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: C:\Users\magoerlich\source\repos\BlazorSandbox\BlazorSandbox\
Since Microsoft.Hosting
has not a specified log level, it inherits the Default
one and so we see these logs.
But we don't see any Microsoft.AspNetCore
entries, since the log level is set to warning
and there currently should'nt be any warnings in your application.
{
...
"Logging": {
...
"Microsoft.AspNetCore": "Information"
...
}
}
Now lets restart the application and watch a flood of loggs flow through our CMD Window:
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[63]
User profile is available. Using 'C:\Users\magoerlich\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
info: Microsoft.Hosting.Lifetime[14]
Now listening on: https://localhost:7252
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5252
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: C:\Users\magoerlich\source\repos\BlazorSandbox\BlazorSandbox\
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
Request starting HTTP/2 POST https://localhost:7252/_blazor/negotiate?negotiateVersion=1 text/plain;charset=UTF-8 0
info: Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler[7]
Identity.Application was not authenticated. Failure message: Unprotect ticket failed
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
Executing endpoint '/_blazor/negotiate'
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
Executed endpoint '/_blazor/negotiate'
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
Request finished HTTP/2 POST https://localhost:7252/_blazor/negotiate?negotiateVersion=1 text/plain;charset=UTF-8 0 - 200 316 application/json 60.4700ms
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
Request starting HTTP/1.1 GET https://localhost:7252/_blazor?id=U9EOBELNcAdSNsKl2ac1UA - -
...
...
Your output might vary slightly, but the important change you should be able to observe is that there are more logs than before
and all of them Start with info: Microsoft.AspNetCore ...
.
These messages were supresed through the config before.
Log Level
There are more log levels than just Information
and Warning
.
Each with its own semantics and verbosity.
Here's a list, try a few of these with the Microsoft.AspNet
Category to experiment:
- None
- Critical
- Error
- Warning
- Information
- Debug
- Trace
You can find further information about the semantics of these levels in the .NET Core Documentation
Thats it for today. In the next part, we'll take a look at how to use all of this when writing logs from your own code.