簡體   English   中英

Logger.LogDebug 與 Debug.WriteLine

[英]Logger.LogDebug vs Debug.WriteLine

我有這個程序

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 調試輸出的DebugView

這是意料之中的,因為據我了解,當我們編譯程序時,我們會得到調試日志。 但是當我使用dotnet run -c Release在版本中編譯它時,我仍然在控制台中得到它

dbug: debuglogtest.Worker[0] _logger.LogDebug

但在 DebugView 中什么都沒有:

發布輸出的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 class 寫入日志 output。 調用 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 中,但在調試和發布模式下都不會出現。

ILogger.LogDebugILogger.LogTrace等是關於消息的LogLevel ,而不是關於它的寫入位置,基本上相當於使用相應的logLevel參數值調用Log(..) 日志消息的寫入目的地取決於您使用的記錄器(控制台、文件、ETW,我相信您甚至可以創建一個寫入調試視圖的記錄器,請參閱文檔的日志記錄提供程序部分)。 LogLevel允許您過濾實際寫入日志目標的日志級別,並由 appsettings 而非直接由構建配置進行調節(盡管您可以為不同的配置設置不同的設置)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM