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.LogDebug
is written both in console and in DebugView when compiling in debug, and no places when compiling in Release Debug.WriteLine
is written in DebugView when compiling in debug What is actually happening:
_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.WriteLine
and 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.
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).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.