c# - Logger.LogDebug 与 Debug.WriteLine

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

有了这个 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/

    相关文章:

    java - 如何停止 log4j 记录日志

    entity-framework - EF6 实体数据模型设计器不适用于目标框架 .net 5.0

    c# - razor 页面,如何将 json 序列化字符串传递到模型属性中

    c# - 升级到 VS 2017 15.6.0,现在 Directory.GetCurrentDirectory() 返回\bin\Debug\netcoreapp2.0

    c# - 在单击 RadioButton 之前保持按钮不可见 c#

    c# - Web Api 变量参数

    nginx limit_req_log_level - 日志在哪里?

    c# - 报表查看器文本框可见性表达式

    c# - 在 Configure 方法中使用 DependencyInjection

    ruby-on-rails - logger.debug vs 放入 Rails 调试?