【问题标题】:Logger.LogDebug vs Debug.WriteLineLogger.LogDebug 与 Debug.WriteLine
【发布时间】:2020-05-31 19:50:57
【问题描述】:

我有这个程序

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

和这个工人

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

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

dbug: debuglogtest.Worker[0] _logger.LogDebug

但 DebugView 中什么也没有:

我的期望:

  1. _logger.LogDebug在debug编译的时候在console和DebugView都写,在Release编译的时候没有地方
  2. Debug.WriteLine在debug编译时写在DebugView中

实际发生了什么:

  1. _logger.LogDebug 只写入控制台,调试模式下不写入 DebugView

Debug.WriteLine 仅在调试模式下编译时正确写入。

我以为LogDebug 在后台调用Debug.WriteLine,但也许Debug.WriteLineLogger.LogDebug 是两个不同的东西?我肯定看起来像。我认为这很令人困惑,因为我们需要在两个不同的地方(编译和过滤时)控制调试日志。如果我阅读文档:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-3.1#debug 它说它调用相同的Debug.WriteLine

调试提供程序使用 System.Diagnostics.Debug 类写入日志输出。调用 System.Diagnostics.Debug.WriteLine 写入 Debug 提供程序。

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

更新

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

【问题讨论】:

    标签: c# logging .net-core


    【解决方案1】:

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

    【讨论】:

    • 这是有道理的,但CreateDefaultHost 仍然添加了.AddDebug()。这如何适应日志记录?我不能使用Logger 写入显示在 DebugView 中的源吗?
    • 实际上在我的脑海中没有任何意义。它通过.AddDebug() 添加了DebugProvider,它应该出现在DebugView 中。它显示在控制台中,因为应用了.AddConsole 并且使用#define DEBUG 它实际上应该以发布模式显示(理论上)。只有应用了某种过滤(我猜有)。我认为这是一种奇怪的行为。
    • 查看日志docsDebugLogger,描述为A logger that writes messages in the debug output window only when a debugger is attached.
    • 我完全错过了那一行 :) 我已经阅读了很多这些文档(请参阅我添加了相同链接的更新)。你说的对。我不知道怎么会错过github.com/aspnet/Logging/blob/master/src/…。代码从不说谎。谢谢!现在一切都说得通了。由于#define DEBUG 在发布模式下,DebugLogger 激活了写入调试,但阻止它的是第 44 行。再次感谢!
    • @mslot 很乐意提供帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多