当目标设置为异步时,NLog 不会刷新缓冲日志
NLog does not flush buffered logs when targets are set as async
我目前正在修改我们的 NLog 配置,并在尝试将我们的目标设置为异步并使用 BufferingTarget 进行邮件处理时,我发现 Nlog 似乎不会在应用程序关闭时刷新邮件。
调用 LogManager.Flush();
也不起作用,它给出 NLog.NLogRuntimeException
:
A first chance exception of type 'NLog.NLogRuntimeException' occurred
in NLog.dll Additional information: Asynchronous exception has
occurred.
我发现从 targets
中删除 async="true"
使我的配置有效。下面是我用来做测试的配置和控制台源代码。
我看到有很多 post 可能与此有关,关于日志序列 and/or FallbackTarget,但它们对解决我遇到的问题没有多大帮助目前。
控制台应用程序
class Program
{
static void Main(string[] args)
{
Logger logger = LogManager.GetCurrentClassLogger();
logger.Debug("Debug Message");
logger.Debug("Debug Message");
LogManager.Flush();
LogManager.Shutdown();
}
}
Nlog 配置
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
internalLogFile="Logs\Nlogs.txt"
throwExceptions="true">
<variable name="LongLayout" value="${longdate} | ${machinename} | ${logger} | ${callsite} (${callsite-linenumber}) | ${message}${onexception:${newline}${exception:format=Type}${newline}${newline}${exception:format=tostring}}"/>
<variable name="ShortLayout" value="${longdate} | ${callsite} (${callsite-linenumber}) | ${message}${onexception:${newline}${exception:format=Type}${newline}${newline}${exception:format=tostring}}"/>
<targets async="true">
<target name="Console" xsi:type="ColoredConsole" layout="${ShortLayout}"/>
<target name="File" xsi:type="File"
fileName="${basedir}\Logs${level}.log"
archiveFileName="${basedir}\Logs\Archives${level}_${shortdate}_{##}.log"
archiveAboveSize="1000000"
layout="${LongLayout}"/>
<target name="MailBuffer" xsi:type="BufferingWrapper" flushTimeout="50000" slidingTimeout="false">
<target name="Mail" xsi:type="Mail"
smtpServer="smtp.gmail.com"
smtpPort="587"
smtpAuthentication="Basic"
smtpUserName="username"
smtpPassword="password"
enableSsl="true"
from="from"
to="to"
subject="Service: ${machinename} | ${logger} | ${level}"
body="${LongLayout}${newline}"/>
</target>
</targets>
<rules>
<logger name="*" levels="Fatal,Error,Warn,Debug,Info,Trace" writeTo="Console" enabled="true" />
<logger name="*" levels="Fatal,Error,Warn,Debug,Info,Trace" writeTo="File" enabled="true" />
<logger name="*" levels="Fatal,Error,Warn,Debug,Info,Trace" writeTo="MailBuffer" enabled="true" />
</rules>
</nlog>
异常堆栈跟踪
at NLog.Common.AsyncHelpers.RunSynchronously(AsynchronousAction action)
at NLog.LogFactory.Flush(TimeSpan timeout)
at NLog.LogFactory.Flush()
at NLog.LogManager.Flush()
at MyProject.NlogTests.Program.Main(String[] args) in p:...\Program.cs:line 15
Nlog 日志文件(简体)
2016-12-09 17:29:55.0471 Trace Opening \bin\Dev\Logs\Info.log with allowFileSharedWriting=False
2016-12-09 17:29:55.5354 Trace LogFactory.Flush(00:00:15)
2016-12-09 17:29:55.5354 Trace Flushing all targets...
2016-12-09 17:29:55.5354 Trace ForEachItemInParallel() 3 items
2016-12-09 17:29:55.5819 Trace Flushing 1 events.
2016-12-09 17:29:55.6119 Trace Opening \bin\Dev\Logs\Info.log with allowFileSharedWriting=False
2016-12-09 17:29:55.6209 Trace Continuation invoked:
2016-12-09 17:29:55.6209 Trace Parallel task completed. 2 items remaining
2016-12-09 17:29:55.6825 Trace Flushing 0 events.
2016-12-09 17:29:55.6825 Trace Continuation invoked:
2016-12-09 17:29:55.6825 Trace Parallel task completed. 1 items remaining
2016-12-09 17:30:16.7442 Trace LogFactory.Flush(00:00:15)
2016-12-09 17:30:16.7442 Trace Flushing all targets...
2016-12-09 17:30:16.7702 Trace ForEachItemInParallel() 3 items
2016-12-09 17:30:16.8032 Trace Flushing 0 events.
2016-12-09 17:30:16.8032 Trace Flushing 0 events.
2016-12-09 17:30:16.8032 Trace Continuation invoked:
2016-12-09 17:30:16.8032 Trace Parallel task completed. 2 items remaining
2016-12-09 17:30:16.8032 Trace Continuation invoked:
2016-12-09 17:30:16.8032 Trace Continuation invoked:
2016-12-09 17:30:16.8167 Trace Parallel task completed. 1 items remaining
2016-12-09 17:30:16.8167 Trace Parallel task completed. 0 items remaining
2016-12-09 17:30:16.8167 Info Shutting down logging...
2016-12-09 17:30:16.8167 Info Closing old configuration.
2016-12-09 17:30:16.8167 Trace LogFactory.Flush(00:00:15)
2016-12-09 17:30:16.8167 Trace Flushing all targets...
2016-12-09 17:30:16.8167 Trace ForEachItemInParallel() 3 items
2016-12-09 17:30:16.8457 Trace Using basic authentication: Username='########@########.com' Password='********'
2016-12-09 17:30:16.8472 Debug Sending mail to ########@########.com using smtp.gmail.com:587 (ssl=True)
2016-12-09 17:30:16.8472 Trace Subject: 'Service: ####### | ######### | Info'
2016-12-09 17:30:16.8472 Trace From: '########@########.com'
我看到默认超时是 15 秒。
您可以尝试增加超时时间。
例如
LogManager.Flush(TimeSpan.FromSeconds(60));
NLog 版本。 4.4.1 现在尝试立即处理手动刷新,而不是等待 BufferingWrapper 的 flushTimeout="50000"。
我目前正在修改我们的 NLog 配置,并在尝试将我们的目标设置为异步并使用 BufferingTarget 进行邮件处理时,我发现 Nlog 似乎不会在应用程序关闭时刷新邮件。
调用 LogManager.Flush();
也不起作用,它给出 NLog.NLogRuntimeException
:
A first chance exception of type 'NLog.NLogRuntimeException' occurred in NLog.dll Additional information: Asynchronous exception has occurred.
我发现从 targets
中删除 async="true"
使我的配置有效。下面是我用来做测试的配置和控制台源代码。
我看到有很多 post 可能与此有关,关于日志序列 and/or FallbackTarget,但它们对解决我遇到的问题没有多大帮助目前。
控制台应用程序
class Program
{
static void Main(string[] args)
{
Logger logger = LogManager.GetCurrentClassLogger();
logger.Debug("Debug Message");
logger.Debug("Debug Message");
LogManager.Flush();
LogManager.Shutdown();
}
}
Nlog 配置
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
internalLogFile="Logs\Nlogs.txt"
throwExceptions="true">
<variable name="LongLayout" value="${longdate} | ${machinename} | ${logger} | ${callsite} (${callsite-linenumber}) | ${message}${onexception:${newline}${exception:format=Type}${newline}${newline}${exception:format=tostring}}"/>
<variable name="ShortLayout" value="${longdate} | ${callsite} (${callsite-linenumber}) | ${message}${onexception:${newline}${exception:format=Type}${newline}${newline}${exception:format=tostring}}"/>
<targets async="true">
<target name="Console" xsi:type="ColoredConsole" layout="${ShortLayout}"/>
<target name="File" xsi:type="File"
fileName="${basedir}\Logs${level}.log"
archiveFileName="${basedir}\Logs\Archives${level}_${shortdate}_{##}.log"
archiveAboveSize="1000000"
layout="${LongLayout}"/>
<target name="MailBuffer" xsi:type="BufferingWrapper" flushTimeout="50000" slidingTimeout="false">
<target name="Mail" xsi:type="Mail"
smtpServer="smtp.gmail.com"
smtpPort="587"
smtpAuthentication="Basic"
smtpUserName="username"
smtpPassword="password"
enableSsl="true"
from="from"
to="to"
subject="Service: ${machinename} | ${logger} | ${level}"
body="${LongLayout}${newline}"/>
</target>
</targets>
<rules>
<logger name="*" levels="Fatal,Error,Warn,Debug,Info,Trace" writeTo="Console" enabled="true" />
<logger name="*" levels="Fatal,Error,Warn,Debug,Info,Trace" writeTo="File" enabled="true" />
<logger name="*" levels="Fatal,Error,Warn,Debug,Info,Trace" writeTo="MailBuffer" enabled="true" />
</rules>
</nlog>
异常堆栈跟踪
at NLog.Common.AsyncHelpers.RunSynchronously(AsynchronousAction action)
at NLog.LogFactory.Flush(TimeSpan timeout)
at NLog.LogFactory.Flush()
at NLog.LogManager.Flush()
at MyProject.NlogTests.Program.Main(String[] args) in p:...\Program.cs:line 15
Nlog 日志文件(简体)
2016-12-09 17:29:55.0471 Trace Opening \bin\Dev\Logs\Info.log with allowFileSharedWriting=False
2016-12-09 17:29:55.5354 Trace LogFactory.Flush(00:00:15)
2016-12-09 17:29:55.5354 Trace Flushing all targets...
2016-12-09 17:29:55.5354 Trace ForEachItemInParallel() 3 items
2016-12-09 17:29:55.5819 Trace Flushing 1 events.
2016-12-09 17:29:55.6119 Trace Opening \bin\Dev\Logs\Info.log with allowFileSharedWriting=False
2016-12-09 17:29:55.6209 Trace Continuation invoked:
2016-12-09 17:29:55.6209 Trace Parallel task completed. 2 items remaining
2016-12-09 17:29:55.6825 Trace Flushing 0 events.
2016-12-09 17:29:55.6825 Trace Continuation invoked:
2016-12-09 17:29:55.6825 Trace Parallel task completed. 1 items remaining
2016-12-09 17:30:16.7442 Trace LogFactory.Flush(00:00:15)
2016-12-09 17:30:16.7442 Trace Flushing all targets...
2016-12-09 17:30:16.7702 Trace ForEachItemInParallel() 3 items
2016-12-09 17:30:16.8032 Trace Flushing 0 events.
2016-12-09 17:30:16.8032 Trace Flushing 0 events.
2016-12-09 17:30:16.8032 Trace Continuation invoked:
2016-12-09 17:30:16.8032 Trace Parallel task completed. 2 items remaining
2016-12-09 17:30:16.8032 Trace Continuation invoked:
2016-12-09 17:30:16.8032 Trace Continuation invoked:
2016-12-09 17:30:16.8167 Trace Parallel task completed. 1 items remaining
2016-12-09 17:30:16.8167 Trace Parallel task completed. 0 items remaining
2016-12-09 17:30:16.8167 Info Shutting down logging...
2016-12-09 17:30:16.8167 Info Closing old configuration.
2016-12-09 17:30:16.8167 Trace LogFactory.Flush(00:00:15)
2016-12-09 17:30:16.8167 Trace Flushing all targets...
2016-12-09 17:30:16.8167 Trace ForEachItemInParallel() 3 items
2016-12-09 17:30:16.8457 Trace Using basic authentication: Username='########@########.com' Password='********'
2016-12-09 17:30:16.8472 Debug Sending mail to ########@########.com using smtp.gmail.com:587 (ssl=True)
2016-12-09 17:30:16.8472 Trace Subject: 'Service: ####### | ######### | Info'
2016-12-09 17:30:16.8472 Trace From: '########@########.com'
我看到默认超时是 15 秒。
您可以尝试增加超时时间。
例如
LogManager.Flush(TimeSpan.FromSeconds(60));
NLog 版本。 4.4.1 现在尝试立即处理手动刷新,而不是等待 BufferingWrapper 的 flushTimeout="50000"。