Nlog AutoFlushWrapper 在带有数据库目标的 BufferingWrapper 之上,如果应用程序 crashes/closes 异常,缓冲区中的日志将丢失

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

我在 AutoFlushWrapper 之上使用 Nlog AutoFlushWrapper,它有 database 目标,fallbackfile 目标,目前我的缓冲区大小为 50flushtimeout 到 2 分钟,但如果应用程序 crashes/closes 异常,则不会记录缓冲区中的日志。

例如 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>

if the application crashes/closes abnormally then the logs in buffer aren't logged.

让程序自行崩溃听起来有点不受欢迎,但它会建议在 finally 中使用 NLog.LogManager.Shutdown();

例如

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();
        }
    }
}