当目标设置为异步时,NLog不会刷新缓冲日志

时间:2016-12-09 16:24:26

标签: c# nlog

我正在修改我们的NLog配置,在尝试设置我们的目标异步并使用BufferingTarget进行邮件处理时,我发现Nlog似乎没有在应用程序关闭时刷新邮件。

调用LogManager.Flush();也不起作用,它会提供NLog.NLogRuntimeException

  

发生了'NLog.NLogRuntimeException'类型的第一次机会异常   在NLog.dll中附加信息:异步异常有   发生。

我发现从async="true"中移除targets会使我的配置生效。下面是我用来进行测试的配置和控制台源代码。

我看到有很多帖子可能与这个帖子有关,关于日志序列和/或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'

2 个答案:

答案 0 :(得分:2)

我看到默认超时是15秒。

您可以尝试增加超时时间。

e.g。

LogManager.Flush(TimeSpan.FromSeconds(60)); 

答案 1 :(得分:1)

NLog ver。 4.4.1现在尝试立即处理手动刷新,而不是等待BufferingWrapper的flushTimeout =“50000”。