【问题标题】:NLog not logging debug messagesNLog 不记录调试消息
【发布时间】:2020-02-13 15:44:57
【问题描述】:

我在 .NET Core 3.1 Worker Service 项目中记录调试级别消息时遇到问题。我的文件目标或控制台都没有记录调试级别的消息。信息级事件按预期写入。我已经查看了older question with the same title 以确保所有这些框都被选中。

NLog.config

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="false"
      internalLogLevel="Off" internalLogFile="%appdata%\FileTransferService\nlog-internal.log">

  <variable name="logdir" value="${specialfolder:folder=ApplicationData}/FileTransferService"/>
  <variable name="stdlayout" value="${longdate}|${level:uppercase=true}|${message:exceptionSeparator=|}${exception:format=ToString}"/>

  <targets>  
    <target name="default" xsi:type="AsyncWrapper" overflowAction="Block">
      <target name="defaultlog" xsi:type="File"
              fileName="${logdir}/dftslog.txt"
              layout="${stdlayout}"
              archiveEvery="Month" concurrentWrites="false"/>
    </target>
  </targets>

  <rules>
    <logger name="*" minlevel="Debug" writeTo="default"/>
  </rules>
</nlog>

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  }
}

我尝试将 Microsoft 和 Microsoft.Hosting.Lifetime 都设置为 Debug,但没有效果。

Program.CreateHostBuilder 方法

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .UseWindowsService()
        .ConfigureLogging((context, logBuilder) => {
            logBuilder.ClearProviders();
            logBuilder.AddConfiguration(context.Configuration.GetSection("Logging"));
            logBuilder.SetMinimumLevel(LogLevel.Debug);
            logBuilder.AddConsole();
            logBuilder.AddNLog();
        })
        .ConfigureServices((hostContext, services) => {
            services.AddHostedService<Worker>();
            services.AddTransient(typeof(ITransferService), typeof(TransferService));
        });

以下是一些附加说明:

  • 我尝试调用 SetMinimumLevel,但没有任何效果。
  • NLog.config 设置为始终复制。
  • 在没有 AddConsole 的情况下,我得到了相同的结果。这实际上是在我看到没有记录调试消息后添加的。

【问题讨论】:

  • 此示例应用程序在记录调试和跟踪方面没有问题:github.com/NLog/NLog.Extensions.Logging/blob/master/examples/…
  • 不幸的是,存储库的内容没有解决任何问题。我将在后续评论中详细介绍我尝试过的内容。
  • 虽然存储库是针对 Rolf K. 提供的 .NET Core 2 的,但我认为它可能仍然有帮助。我马上注意到他能够在构建器中使用 UseNLog 而不是 AddNLog。原来是因为我添加了包 NLog.Extensions.Logging 而不是 NLog.Extensions.Hosting。我还简化了我的配置,所以它所做的只是调用 SetMinimumLevel。问题依然存在。接下来,我完全删除了 appsettings.json 的内容。那也没有解决问题。我还在 finally 块中添加了对 LogManager.Shutdown 的调用,但我没想到这很重要。
  • 您可以在这里创建一个新问题github.com/NLog/NLog.Extensions.Logging/issues/new 并附上您无法登录的示例应用程序的源代码
  • 也许解决方案就在这个答案中?stackoverflow.com/a/59777331/201303

标签: c# logging .net-core nlog


【解决方案1】:

当 NLog 与 Microsoft-Extension-Logging (MEL) 一起使用时,NLog 将成为 MEL-LoggerFactory 中的 LoggingProvider。

这意味着在 MEL-LoggerFactory 中配置的任何过滤总是获胜,独立于 NLog.config。

MEL-LoggerFactory 将自动从appsettings.json(以及任何特定于环境的appsettings.development.json 或appsettings.production.json)加载其过滤。

即使调用了SetMinimumLevel(LogLevel.Debug),appsettings.json 中的任何配置都会覆盖它。

另请参阅:https://github.com/NLog/NLog.Web/wiki/Missing-trace%5Cdebug-logs-in-ASP.NET-Core-3%3F

【讨论】:

  • appsettings.development.json 让我明白了 - 它在此处默认为信息,而不是跟踪/调试
【解决方案2】:

问题似乎出在 Visual Studio 2019 上。

虽然问题本身仍然存在,但当我在 Visual Studio 之外运行应用程序时,调试级别事件会同时写入控制台和平面文件。这不是我预期的调试器造成的,因为如果我在 Visual Studio 中不使用调试器运行它,它会表现出相同的行为。

我查看了 Visual Studio 设置并进行了快速搜索,但找不到任何相关内容。也就是说,知道事件将在 Visual Studio 之外记录,可以在必要的范围内解决我的问题。

【讨论】:

  • 请注意,您的项目可能有多个 appsettings.json 文件。每个环境一个。前任。 appsettings.development.json、appsettings.Production.json 等。另见docs.microsoft.com/en-us/aspnet/core/fundamentals/environments(还有一种叫launchSettings.json)
  • 谢谢。我总是确保我的 appsettings.development.json 匹配。我没有看到关于 launchSettings.json 的任何内容,但那篇文章确实提到了 ASP.NET Core 而不是 .NET Core。
  • 非常感谢!我花了很长时间才发现这一点!
【解决方案3】:

要添加到已接受的答案(没有足够的评论点数):

同样的问题。但是当我使用时,它也可以在 VS2019 中正常工作

private static Logger _logger = LogManager.GetCurrentClassLogger();

我更喜欢 DI 来获取我的记录器(主要是因为我喜欢写 .Warn(...) 而不是 .LogWarning(...)

我仍然为 NLog 设置 DI,以便外部/ms 库将使用它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 2023-03-04
    相关资源
    最近更新 更多