Logger.LogDebug 与 Debug.WriteLine

msl*_*lot 5 c# logging .net-core

我有这个程序

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>();
                });
    }
}
Run Code Online (Sandbox Code Playgroud)

和这个工人

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");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

和这个配置

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

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

调试:debuglogtest.Worker[0] _logger.LogDebug

这在 DebugView 中 调试输出的 DebugView

这是预期的,因为据我了解,当我们编译程序时,我们会得到调试日志记录。但是当我在发行版中编译它时,dotnet run -c Release我仍然在控制台中看到它

调试:debuglogtest.Worker[0] _logger.LogDebug

但 DebugView 中没有任何内容:

发布输出的 DebugView

我期望什么:

  1. _logger.LogDebug在debug中编译时,控制台和DebugView中都有写,在Release中编译时没有地方
  2. Debug.WriteLine在debug中编译时写在DebugView中

实际发生了什么:

  1. _logger.LogDebug仅写入控制台,而不是调试模式下的 DebugView

Debug.WriteLine只有在调试模式下编译时才能正确写入。

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

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

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

更新

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

Gur*_*ron 2

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