question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:41 (22 by maintainers)

github_iconTop GitHub Comments

2reactions
snakefootcommented, Aug 24, 2019

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.
2reactions
kirya522commented, Dec 16, 2019

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

NLog Async target timestamps
I know that according to What's the meaning of the time stamp in nlog when async is on? the timestamp is generated when...
Read more >
Logging Cookbook — Python 3.11.4 documentation
This page contains a number of recipes related to logging, which have been found useful in ... Let's also assume that the file...
Read more >
Log4j 2 Appenders - Apache Logging Services
The AsyncAppender accepts references to other Appenders and causes ... Date , then the log timestamp will be converted to that configured date...
Read more >
Application Insights API for custom events and metrics
Use the Application Insights core telemetry API to send custom events and metrics and your own versions of standard telemetry.
Read more >
Logging configuration
Configure the JSON logging extension using supported properties to customize its behavior. Determine whether to enable the JSON console formatting extension, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found