I was struggling for about an hour getting debug logging to console working in ASP.NET Core so I thought I should write it down. I got tricked by the default appsettings.json
and appsettings.Development.json
that get generated when you run dotnet new
. First appsettings.json
:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
}
}
Pretty straightforward: default log levels for debug and console are set to Warning
. And now appsettings.Development.json
:
1
2
3
4
5
6
7
8
9
10
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
The way I interpret this, but which is apparently wrong, is as follows: in a development environment the default log level is Debug
so if I do LogDebug
, it will appear on stdout. Well, it does not… (otherwise I would not have written this post)
I think this is counter-intuitive, especially since this is the default that gets generated when you run dotnet new
. Why have this default when it does not result in debug logging? And what does this default accomplish anyhow?
What you need to do in appsettings.Development.json
is explicitly configure console logging and set the desired logging levels:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"Logging": {
"IncludeScopes": true,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
},
"Console": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
}
I still do not quite understand what the default log level on line 5 does. The keyword Console
on line 9 refers to the console logging provider. There are a number of other logging providers but there is no such thing as a ‘default logging provider’. After some more careful reading of the documentation, it appears that the default filter rule applies to ‘all other providers’. These are the providers that you do not explicitly specify in your appsettings.json
or appsettings.Development.json
files.
Now it begins to make sense I guess: the two configuration files are merged and the most specific rule is selected. In the case of the settings files that are generated by default, this means that the console rule with log level warning is selected. You can override this by specifying another console rule in appsettings.Development.json
.
The original article was posted on: ronaldwildenberg.com