NLog 不记录调试消息



我在 .NET Core 3.1 Worker Service 项目中记录调试级别消息时遇到问题。我的文件目标或控制台都没有记录调试级别消息。信息级事件按预期写入。我已经审查了一个具有相同标题的旧问题,以确保选中所有这些框。

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%FileTransferServicenlog-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也设置为调试,但这没有效果。

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,我得到相同的结果。这实际上是在我看到调试消息未被记录后添加的。

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

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

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

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

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

问题似乎出在Visual Studio 2019上。

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

我浏览了Visual Studio设置并进行了快速搜索,但找不到任何相关内容。也就是说,知道事件将在Visual Studio之外记录,可以在一定程度上解决我的问题。

要添加到接受的答案中(没有足够的分数进行评论(:

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

private static Logger _logger = LogManager.GetCurrentClassLogger();

我更喜欢 DI 来获取我的记录器(主要是因为我喜欢写作.警告(...( 结束 .日志警告(...(

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

Rolf给出了正确的答案。 下面是一个示例代码,允许您在 DEBUG 会话期间添加跟踪级别。

_services.AddLogging(loggingBuilder =>
{
#if DEBUG
// Add Trace/Debug statements to the output.
loggingBuilder.Services.TryAddEnumerable(                    
ServiceDescriptor.Singleton<IConfigureOptions<LoggerFilterOptions>>(
new ConfigureOptions<LoggerFilterOptions>(
options => options.MinLevel = LogLevel.Trace)));
#endif
loggingBuilder.AddNLog(NLog.LogManager.Configuration);
});

相关内容

最新更新