Slow performance using Nlog when compared to log4net

3.4k Views Asked by At

We recently updated the logging framework for our ASP.NET Core Web API project from log4net to NLog and when we do performance testing request/second rate served NLog is way less when compared it to NLog. Below is my NLog configuration file.

Please suggest any ideas on our performance issue using NLog.

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      internalLogLevel="info"
      internalLogFile="C:\temp\internal-nlog.txt">

  <!-- enable asp.net core layout renderers -->
  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
    <add assembly="SumoLogic.Logging.NLog"/>
  </extensions>

  <!-- the targets to write to -->
  <targets>
    <!-- write logs to console -->
    <target xsi:type="ColoredConsole" name="allConsole" formatMessage="false" layout="${longdate}|${level:uppercase=true}|${message}" />

    <target name="sumoLogic" type="SumoLogicTarget" formatMessage="false" layout="${date:format=yyyy-MM-dd HH\:mm\:ss.fff} ${level}, ${message}${exception:format=tostring}${newline}">
      <Url>#{Logging__SumoLogic__EndpointUrl}</Url>
      <ConnectionTimeout>30000</ConnectionTimeout>
      <SourceName>#{Logging__SumoLogic__SourceName}</SourceName>
      <SourceCategory>#{Logging__SumoLogic__SourceCategory}</SourceCategory>
      <UseConsoleLog>true</UseConsoleLog>
    </target>

    <target name="bufferedSumoLogic" type="BufferedSumoLogicTarget" formatMessage="false" layout="${date:format=yyyy-MM-dd HH\:mm\:ss.fff} ${level}, ${message}${exception:format=tostring}${newline}">
      <Url>#{Logging__SumoLogic__EndpointUrl}</Url>
      <SourceName>#{Logging__SumoLogic__SourceName}</SourceName>
      <SourceCategory>#{Logging__SumoLogic__SourceCategory}</SourceCategory>
      <ConnectionTimeout>30000</ConnectionTimeout>
      <RetryInterval>5000</RetryInterval>
      <MessagesPerRequest>10</MessagesPerRequest>
      <MaxFlushInterval>10000</MaxFlushInterval>
      <FlushingAccuracy>250</FlushingAccuracy>
      <MaxQueueSizeBytes>500000</MaxQueueSizeBytes>
      <UseConsoleLog>true</UseConsoleLog>
    </target>
  </targets>

  <!-- rules to map from logger name to target -->
  <rules>
    <!-- Skip non-critical Microsoft logs and so log only own logs -->
    <logger name="Microsoft.*" maxLevel="Info" final="true" />

    <!-- Write all debug messages to console -->
    <logger name="*" minlevel="#{Logging__ConsoleLog__LevelMin}" writeTo="allConsole" />

    <!-- Write to sumo buffered log -->
    <logger name="*" minlevel="#{Logging__BufferedLog__LevelMin}" maxlevel="#{Logging__BufferedLog__LevelMax}" writeTo="bufferedSumoLogic" />

    <!-- Write to sumo instant log -->
    <logger name="*" minlevel="#{Logging__InstantLog__LevelMin}" maxlevel="#{Logging__InstantLog__LevelMax}" writeTo="sumoLogic" />
  </rules>
</nlog>
1

There are 1 best solutions below

0
On

Unluckily you can't speed up what the targets do inside, they could be doing web requests, db calls, writing to files, etc. (and I guess that's OK after all) BUT you can avoid blocking the application thread from which you are logging, that's for me the important matter here.

Just add async="true" to your targets element, it should read: <targets async="true">

A little explanation from the link below:

Asynchronous target wrapper allows the logger code to execute more quickly, by queuing messages and processing them in a separate thread. You should wrap targets that spend a non-trivial amount of time in their Write() method with asynchronous target to speed up logging.

Also have in mind that some targets don't work well in async mode (you'll discover by testing, in my case RollbarSharp), so you can create another targets section for those ones.

You can get more info here: https://github.com/nlog/NLog/wiki/AsyncWrapper-target

Good luck!