gpt4 book ai didi

c# - Logger.LogDebug 与 Debug.WriteLine

转载 作者:行者123 更新时间:2023-12-04 15:26:38 24 4
gpt4 key购买 nike

我有这个程序

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>();
});
}
}

有了这个 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");
}
}
}

和这个配置
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}

当我在调试中运行时 dotnet run -c Debug我在控制台中得到了这个

dbug: debuglogtest.Worker[0] _logger.LogDebug



这在 DebugView
DebugView of debug output

这是意料之中的,因为据我所知,当我们编译我们的程序时,我们会得到调试日志。但是当我用 dotnet run -c Release 在发行版中编译它时我仍然在控制台中得到这个

dbug: debuglogtest.Worker[0] _logger.LogDebug



但在 DebugView 中什么都没有:

DebugView of release output

我的期望:
  • _logger.LogDebug在debug中编译时在console和DebugView中都写,在Release中编译时没有地方
  • Debug.WriteLine在调试中编译时在 DebugView 中编写

  • 实际发生的事情:
  • _logger.LogDebug仅写入控制台,而不是 Debug模式下的 DebugView
  • Debug.WriteLine只有在 Debug模式下编译时才能正确写入。

    我以为 LogDebug正在调用 Debug.WriteLine在引擎盖下,但也许 Debug.WriteLineLogger.LogDebug是两个不同的东西吗?我肯定看起来像。我认为这很令人困惑,因为我们需要在两个不同的地方(编译和过滤时)控制调试日志记录。如果我阅读文档: 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. Calls to System.Diagnostics.Debug.WriteLine write to the Debug provider.



    难道我做错了什么?我一定是误会了什么。我希望能够控制 Logger.Debug在 Debug模式下编译时。那不可能吗?

    更新

    如果我检查源(即使它有点旧(有人知道更新的引用,请说明)): https://github.com/aspnet/Logging/blob/master/src/Microsoft.Extensions.Logging.Debug/DebugLogger.debug.cs我可以看到它定义了 #define DEBUG ,并调用 Debug.WriteLine ,但它并没有在我的脑海中加起来。它应该会出现在 DebugView 中,但它不会同时出现在调试和 Release模式中。

    最佳答案

    ILogger.LogDebug , ILogger.LogTrace等等是关于 LogLevel 消息而不是关于它写在哪里,基本上相当于调用 Log(..) 对应值 logLevel范围。日志消息的写入目的地由您使用的记录器决定(控制台、文件、ETW,我相信您甚至可以创建一个写入调试 View 的记录器,请参阅文档的 logging providers section)。 LogLevel允许您过滤实际写入日志目标的日志级别,并由 appsettings 而非构建配置直接管理(尽管您可以为不同的配置设置不同的设置)。

    关于c# - Logger.LogDebug 与 Debug.WriteLine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62121673/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com