繁体   English   中英

如果应用程序崩溃/异常关闭,则在具有数据库目标的BufferingWrapper之上的Nlog AutoFlushWrapper会丢失缓冲区中的日志

[英]Nlog AutoFlushWrapper on top of BufferingWrapper with database target the logs in buffer are lost if application crashes/closes abnormally

我使用Nlog AutoFlushWrapper之上AutoFlushWrapper具有database的目标与fallbackfile的目标,目前我有缓冲区的大小50沿flushtimeout至2分钟,但如果应用程序崩溃/异常关闭,然后在缓冲区中的日志不会被记录。

例如,Nlog在缓冲区中有10日志,应用程序崩溃了,那么这些日志不会被刷新。

有人可以帮我吗?

这是代码的一小段

<target name="main" xsi:type="AutoFlushWrapper" asyncFlush="true" OptimizeBufferReuse="true">
  <target name="database_buffer" xsi:type="BufferingWrapper" bufferSize="50" flushTimeout="120000" OptimizeBufferReuse="true">
    <target xsi:type="FallbackGroup" name="String" returnToFirstOnSuccess="true">
      <target xsi:type="Database" name="database_inner" connectionString="${event-context:item=dbConnectionString}"
              commandText="INSERT INTO [Log] ([Level],[Message],[Application],[MethodInfo],[Exception]) VALUES(@Level,@Message,@ApplicationName,@MethodInfo,@Exception)">
        <parameter name="@Level" layout="${level:uppercase=true}"/>
        <parameter name="@Message" layout="${event-context:item=Message}"/>
        <parameter name="@ApplicationName" layout="${event-context:item=SourceName}"/>
        <parameter name="@MethodInfo" layout="${event-context:item=MethodInfo}"/>
        <parameter name="@Exception" layout="${event-context:item=Exception}"/>
      </target>
      <target xsi:type="File"
              name="fallback"
              fileName="${basedir}Logs\Log.log"
              archiveAboveSize="10485760"
              archiveFileName="${basedir}\Logs\Archive\Log_{####}.log"
              archiveNumbering="Sequence"
              concurrentWrites ="false"
              maxArchiveFiles ="10"
              layout="Date: ${longdate}${newline}
          ApplicationName: ${event-context:item=SourceName}${newline}
          Level: ${level:uppercase=true}${newline}
          MethodInfo: ${event-context:item=MethodInfo}${newline}
          Message: ${event-context:item=Message}${newline}
          Exception: ${event-context:item=Exception}${newline}${newline}" />
    </target>
  </target>
</target>

如果应用程序崩溃/异常关闭,则不会记录缓冲区中的日志。

听起来有点让人NLog.LogManager.Shutdown();让程序本身崩溃,但建议使用NLog.LogManager.Shutdown(); finally

例如

class Program
{
    static void Main(string[] args)
    {
        try
        {
            //do something
        }

        finally
        {
            // Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
            NLog.LogManager.Shutdown();
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM