NLog: Custom target with async attribute get incorrect timestamp and order of logs

NLog version: 4.6.5

Platform: WebApi 2.1 .net47

Current NLog config

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets async="true">
      <target name="graylogt" xsi:type="GraylogHttp" facility="WebApi" graylogServer="http://graylog" graylogPort="12201">
        <parameter name="current_user" layout="${aspnet-user-identity}" />
        <parameter name="exception_type" layout="${exception:Type}" />
        <parameter name="request" layout="${aspnet-request-method} ${aspnet-request-url}" />
        <parameter name="site" layout="${iis-site-name} ${aspnet-appbasepath}" />
        <parameter name="request_body" layout="${onexception:${aspnet-request-posted-body}:when=level>=LogLevel.Error}" />
      </target>
    </targets>
    <rules>
     <logger name="*" minlevel="debug" writeTo="graylogt" />
    </rules>
  </nlog>

Hi, I use a https://github.com/dustinchilson/NLog.Targets.GraylogHttp for graylog. In order to don’t block the thread for logging I use an async attribute. But logs have incorrect timestamps and order(compare to sync mode). I can’t use “sync” logs, because every log operation takes 0.2 sec.

  • What is the current result?

Incorrect order and timestamp of logs

  • What is the expected result?

Correct order and timestamps

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 41 (22 by maintainers)

Most upvoted comments

Created PR https://github.com/farzadpanahi/NLog.GelfLayout/pull/14 for you. Introduces a new setting FixDuplicateTimestamp (Default = False)

Can handle bursts of 9 unique timestamps within the same millisecond:

  • If time-resolution is 16 millisecond, then it will support 500 msgs/sec. with unique timestamp.
  • If time-resolution is 1 millisecond (AccurateUTC), then it will support 9000 msgs/sec with unique timestamp.

So my final nlog-config is

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <time type="AccurateUTC" />
    <extensions>
      <add assembly="NLog.Web" />
      <add assembly="NLog.Layouts.GelfLayout" />
    </extensions>
    <targets async="true">
      <target name="graylogt" xsi:type="network" address="tcp://graylog:12200" newLine ="true" lineEnding="Null" >
        <layout xsi:type="GelfLayout" facility="WebApi">
          <field name="current_user" layout="${aspnet-user-identity}" />
          <field name="exception_type" layout="${exception:Type}" />
          <field name="request" layout="${aspnet-request-method} ${aspnet-request-url}" />
          <field name="site" layout="${iis-site-name} ${aspnet-appbasepath}" />
          <field name="request_body" layout="${onexception:${aspnet-request-posted-body}:when=level>=LogLevel.Error}" />
        </layout>
      </target>
    </targets>
    <rules>
        <logger name="*" minlevel="debug" writeTo="graylogt" />
    </rules>
  </nlog>

and here is my graylog input image

Happy the problem has been resolved. Could you post your final nlog-config for others to witness?

What happens if you configure keepConnection="True" and lineEnding="Null" ?