I’ve been getting to grips with ASP.NET 5 (now ASP.NET Core 1.0) over the past few weeks and recently wanted to log out some debug messages from some custom middleware:
log.LogDebug("Setting current tenant.");
In startup.cs
I added the following to ConfigureServices
:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.MinimumLevel = LogLevel.Debug;
...
}
The default log level in ASP.NET 5 is Information
. However, even after setting the MinimumLevel
to Debug
I still couldn’t see my debug messages.
It turns out that the Console Logger also defaults to LogLevel.Information
which means that the debug messages get dropped. The solution is to explicitly set the log level of the logging provider, in this case:
loggerFactory.AddConsole(LogLevel.Debug);
After doing this, my debug messages were displayed.
Note that you need to set both the ILoggerFactory.MinimumLevel
and logging level of the logging provider to Debug
- it’s confusing I know. I guess this is a “safe by default” setting so that sensitive information doesn’t find it’s way into your production logs.
If passing a configuration section to the logging provider (like the standard ASP.NET 5 MVC template does), set the value for the default category in appsettings.json
:
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},