Logger.LogDebug vs Debug.WriteLine

I have this program

using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace debuglogtest { public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureServices((hostContext, services) => { services.AddHostedService<Worker>(); }); } } 

With this worker

using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; namespace debuglogtest { public class Worker : BackgroundService { private readonly ILogger<Worker> _logger; public Worker(ILogger<Worker> logger) { _logger = logger; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { _logger.LogDebug("_logger.LogDebug"); _logger.LogTrace("_logger.LogTrace"); Debug.WriteLine("Debug.WriteLine"); } } } 

and this configuration

{ "Logging": { "LogLevel": { "Default": "Debug", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } } } 

When I run this in debug dotnet run -c Debug i get this in the console

dbug: debuglogtest.Worker[0] _logger.LogDebug

And this in DebugView DebugView of debug output

This is expected, because as i understand it, when we compile our program, we get get debug logging. But when i compile this in release with dotnet run -c Release I still get this in the console

dbug: debuglogtest.Worker[0] _logger.LogDebug

But nothing in DebugView:

DebugView of release output

What i expect:

  1. _logger.LogDebug is written both in console and in DebugView when compiling in debug, and no places when compiling in Release
  2. Debug.WriteLine is written in DebugView when compiling in debug

What is actually happening:

  1. _logger.LogDebug is only written to console, and not DebugView in debug mode

Debug.WriteLine is correctly written only when compiling in debug mode.

I thought that LogDebug was calling Debug.WriteLine under the hood, but maybe Debug.WriteLineand Logger.LogDebug is two different things? I surely looks like it. I think this is confusing because, we need to control Debug logging in two different places (when compiling and filtering). If i read the docs: it says that it calls the same Debug.WriteLine:

The Debug provider writes log output by using the System.Diagnostics.Debug class. Calls to System.Diagnostics.Debug.WriteLine write to the Debug provider.

Am I doing something wrong? I must be misunderstanding something. I want to be able to control Logger.Debug when compiling in Debug mode. Is that not possible?

UPDATE

If i check the source (even though it is kind of old (does anybody know an updated reference, please specify)): I can see that it defines #define DEBUG, and calls the Debug.WriteLine, but it doesn't add up in my head. It should be showing up in DebugView, but it doesn't in both debug and release mode.

1 Answer

ILogger.LogDebug, ILogger.LogTrace and so on is about LogLevel of message and not about where it is written and is basically equivalent to calling Log(..) with corresponding value of logLevel parameter. The write destination of log message is determined by which logger you are using (console, file, ETW, I'm sure that you can even create one which writes to the debug view see the logging providers section of docs). LogLevel allows you to filter which log level is actually written to the log destination and is regulated by appsettings and not directly by build configuration (though you can have different settings for different configurations).

5

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like