NLog不记录调试消息

问题描述 投票:0回答:1

我在.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,我将得到相同的结果。这实际上是在我看到调试消息没有记录下来之后才添加的。
c# logging .net-core nlog
1个回答
0
投票

问题似乎出在Visual Studio 2019中。

尽管问题仍然存在,但是当我在Visual Studio外部运行应用程序时,调试级别的事件会同时写入控制台和平面文件。这不是由于调试器引起的,因为如果我在Visual Studio中没有调试器的情况下运行该调试器,它将表现出相同的行为。

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

© www.soinside.com 2019 - 2024. All rights reserved.