当执行者被杀死时,NLog 不会记录到数据库

NLog doesn't log to database when executor gets killed

我在一个应用程序中使用 NLog,该应用程序启动一个 worker 可执行文件,从 worker 获取结果,然后 worker 被杀死或自行退出。

这是我使用的配置:

  <nlog autoReload="true" throwExceptions="true" internalLogFile="c:\webapplogs\Nloglog.log" internalLogLevel="Warn" xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <variable name="datasource" value="Data Source=.;Initial Catalog=DEV_DB;Persist Security Info=True;User ID=****" />
    <variable name="logDirectory" value="C:\webapplogs\/logs" />
    <variable name="message_one_line" value="${replace:inner=${message}:searchFor=\r\n|\r|\n:replaceWith=\xB6:regex=true}" />
    <!--Pilcrow = "\xB6";-->
    <variable name="exception_one_line" value="${replace:inner=${exception:format=tostring}:searchFor=\r\n|\r|\n:replaceWith=\xB6:regex=true}" />
    <!--Pilcrow = "\xB6";-->
    <variable name="exception_and_message_with_level" value="${longdate} =&gt; (${uppercase:${level}}) | ${callsite} : ${message_one_line} | ${exception_one_line}" />
    <variable name="trace" value="${longdate} =&gt; ${message_one_line}" />
    <targets>
      <!-- https://github.com/nlog/NLog/wiki/File-target -->
      <target name="logfile" type="File" fileName="${logDirectory}/${shortdate}.${logger}.CmdLineWorker.Tracing.log" layout="${exception_and_message_with_level}" keepFileOpen="True" concurrentWrites="True" openFileCacheTimeout="30" />
      <target name="tracefile" type="File" fileName="${logDirectory}/${shortdate}.${logger}.log" layout="${trace}" keepFileOpen="True" concurrentWrites="True" openFileCacheTimeout="30" />
      <target name="console" type="Console" layout="${message}" />
      <target name="database" type="Database" connectionString="${datasource}" commandText="exec dbo.Error_Insert @********">
        <parameter name="@ErrDate" layout="${date:format=yyyy-MM-dd HH\:mm\:ss}" />
        <parameter name="@ErrUser" layout="CmdLineWorker" />
        <parameter name="@LogLevel" layout="${level}" />
        <parameter name="@ErrOrganization" layout="CmdLineWorker" />
        <parameter name="@ErrMessage" layout="${message}" />
        <parameter name="@ErrInnerException" layout="${exception:format=ToString}" />
        <parameter name="@ErrCallsite" layout="${callsite}" />
      </target>
    </targets>
    <rules>
      <logger name="*" minlevel="Trace" writeTo="logfile" />
      <logger name="*" minLevel="Error" writeTo="database" />
    </rules>
  </nlog>

在某些情况下,当出现问题时我会杀死工人。

当我杀死工人时,在日志文件中记录(使用错误级别)效果很好,但问题是,大多数时候,不会在数据库中记录。我在记录后杀死了可执行文件,但它似乎没有直接记录到数据库,所以我在它尝试实际记录错误之前将其杀死。

因此,通过上面的配置,我在日志文件中得到了所需的错误,但在数据库中却没有,这很奇怪。

是否有用于登录数据库的缓冲区?

是否有可以解决此问题的设置?

另外请注意,延迟杀死工人对我来说不是一个很好的选择,所以我想避免它。

退出程序时,需要刷新所有事件:

NLog.LogManager.Shutdown(); // Flush and close down internal threads and timers

写入数据库可能比直接写入文件需要更多时间,因此可以解释差异。

另请参阅:https://github.com/NLog/NLog/wiki/Tutorial

Remember to Flush

NLog will by default attempt to flush automatically at application shutdown. Microsoft Windows give .NET applications a limited amount of time to perform shutdown (usually 2 sec) before being terminated. If having a NLog configuration with NLog Targets that requires network-traffic (Http, Mail, Tcp), then it is a really good idea to perform a manual Flush/Shutdown independent on running on Linux/Windows.

NLog.LogManager.Shutdown(); // Flush and close down internal threads and timers

NET Application running on Mono/Linux are required to stop threads/timers before entering application shutdown phase. Failing to do this will cause unhandled exceptions and segmentation faults, and other unpredictable behavior.