NLog 不记录调试信息
NLog not logging debug messages
我在 .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 也设置为调试,但没有效果。
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 的情况下得到了相同的结果。这实际上是在我看到没有记录调试消息后添加的。
问题似乎与 Visual Studio 2019 年有关。
尽管问题本身仍然存在,但当我 运行 在 Visual Studio 之外的应用程序时,调试级事件被写入控制台和平面文件。这不是我所期望的调试器的原因,因为如果我 运行 它在 Visual Studio.
中没有调试器,它会表现出相同的行为
我查看了 Visual Studio 设置并进行了快速搜索,但找不到任何相关内容。也就是说,知道事件将被记录在 Visual Studio 之外可以在必要的范围内解决我的问题。
要添加到已接受的答案中(评论点数不足):
同样的问题。但是当我使用
时,它在 VS2019 中也能正常工作
private static Logger _logger = LogManager.GetCurrentClassLogger();
我更喜欢 DI 来获取我的记录器(主要是因为我喜欢写 .Warn(...) 而不是 .LogWarning(...)
我仍然为 NLog 设置 DI,因此 external/ms 库将使用它
将 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
罗尔夫答对了。这是一个示例代码,可让您在 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);
});
我在 .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 也设置为调试,但没有效果。
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 的情况下得到了相同的结果。这实际上是在我看到没有记录调试消息后添加的。
问题似乎与 Visual Studio 2019 年有关。
尽管问题本身仍然存在,但当我 运行 在 Visual Studio 之外的应用程序时,调试级事件被写入控制台和平面文件。这不是我所期望的调试器的原因,因为如果我 运行 它在 Visual Studio.
中没有调试器,它会表现出相同的行为我查看了 Visual Studio 设置并进行了快速搜索,但找不到任何相关内容。也就是说,知道事件将被记录在 Visual Studio 之外可以在必要的范围内解决我的问题。
要添加到已接受的答案中(评论点数不足):
同样的问题。但是当我使用
时,它在 VS2019 中也能正常工作private static Logger _logger = LogManager.GetCurrentClassLogger();
我更喜欢 DI 来获取我的记录器(主要是因为我喜欢写 .Warn(...) 而不是 .LogWarning(...)
我仍然为 NLog 设置 DI,因此 external/ms 库将使用它
将 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
罗尔夫答对了。这是一个示例代码,可让您在 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);
});