Skip to main content

Debug logging to console with .NET Core

· 2 min read

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:

appsettings.json
loading...

Pretty straightforward: default log levels for debug and console are set to Warning. And now appsettings.Development.json:

appsettings.Development.json
loading...

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:

appsettings.Development.json
loading...

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.