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
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:
What i expect:
_logger.LogDebugis written both in console and in DebugView when compiling in debug, and no places when compiling in ReleaseDebug.WriteLineis written in DebugView when compiling in debug
What is actually happening:
_logger.LogDebugis 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: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-3.1#debug 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)): https://github.com/aspnet/Logging/blob/master/src/Microsoft.Extensions.Logging.Debug/DebugLogger.debug.cs 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.

