[英]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当我在调试
dotnet run -c Debug
中运行它时,我在控制台中得到它
dbug: debuglogtest.Worker[0] _logger.LogDebug
dbug: debuglogtest.Worker[0] _logger.LogDebug
And this in DebugView这在 DebugView
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但是当我使用
dotnet run -c Release
在版本中编译它时,我仍然在控制台中得到它
dbug: debuglogtest.Worker[0] _logger.LogDebug
dbug: debuglogtest.Worker[0] _logger.LogDebug
But nothing in DebugView:但在 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 _logger.LogDebug
在debug编译的时候在console和DebugView都写,在Release编译的时候没有地方Debug.WriteLine
is written in DebugView when compiling in debug Debug.WriteLine
是在 debug 编译时写在 DebugView 中的What is actually happening:实际发生了什么:
_logger.LogDebug
is only written to console, and not DebugView in debug mode _logger.LogDebug
只写入控制台,在调试模式下不写入 DebugView Debug.WriteLine
is correctly written only when compiling in debug mode.只有在调试模式下编译时才能正确写入
Debug.WriteLine
。
I thought that LogDebug
was calling Debug.WriteLine
under the hood, but maybe Debug.WriteLine
and Logger.LogDebug
is two different things?我认为
LogDebug
在后台调用Debug.WriteLine
,但也许Debug.WriteLine
和Logger.LogDebug
是两个不同的东西? 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
:如果我阅读文档: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-3.1#debug它说它调用相同的
Debug.WriteLine
:
The Debug provider writes log output by using the System.Diagnostics.Debug class.
调试提供程序使用 System.Diagnostics.Debug class 写入日志 output。 Calls to System.Diagnostics.Debug.WriteLine write to the Debug provider.
调用 System.Diagnostics.Debug.WriteLine 写入调试提供程序。
Am I doing something wrong?难道我做错了什么? I must be misunderstanding something.
我一定是误会了什么。 I want to be able to control
Logger.Debug
when compiling in Debug mode.我希望能够在调试模式下编译时控制
Logger.Debug
。 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.如果我检查源(即使它有点旧(有人知道更新的参考,请指定)): https://github.com/aspnet/Logging/blob/master/src/Microsoft.Extensions.Logging。 Debug/DebugLogger.debug.cs我可以看到它定义了
#define DEBUG
,并调用了Debug.WriteLine
,但它并没有在我的脑海中加起来。 It should be showing up in DebugView, but it doesn't in both debug and release mode.它应该出现在 DebugView 中,但在调试和发布模式下都不会出现。
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. ILogger.LogDebug
, ILogger.LogTrace
等是关于消息的LogLevel
,而不是关于它的写入位置,基本上相当于使用相应的logLevel
参数值调用Log(..)
。 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).日志消息的写入目的地取决于您使用的记录器(控制台、文件、ETW,我相信您甚至可以创建一个写入调试视图的记录器,请参阅文档的日志记录提供程序部分)。
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). LogLevel
允许您过滤实际写入日志目标的日志级别,并由 appsettings 而非直接由构建配置进行调节(尽管您可以为不同的配置设置不同的设置)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.