Detailed explanation of. Net core2.0 log component log4net and NLog simple performance test

Time:2019-11-20

Simple performance test of log4net and NLog in. Net core

Compare the file write performance of log4net and NLog (. NETCORE environment). If there is any error in the code and configuration, please criticize and correct it.

testing environment

Development tool: Vsual studio 2017 15.3

Framework version:. Net core 2.0

Operating system: windows10 enterprise 1703

Hardware configuration: CPU i3-4170 3.7GHz, memory 8g, SSD

Log component

log4net 2.0.8

nlog 5.0.0-beta10

test case

1. Do not enable buffer, insert 200000 consecutive lines of string into the file, and the maximum single file size is 1MB.

2. Enable buffer to be 100, insert 200000 consecutive lines of string into the file, and the maximum single file size is 1MB.

test method

XUnit unit test.

Test code

using System;
using System.Diagnostics;
using System.IO;
using Xunit;
using Xunit.Abstractions;
namespace Demo.Logging.Tests
{
  /// <summary>
  ///Log file write comparison between log4net and NLog
  /// </summary>
  public class BigDataTest
  {
    private readonly ITestOutputHelper output;
    public BigDataTest(ITestOutputHelper outputHelper)
    {
      output = outputHelper;
    }

    /// <summary>
    ///Use log4net to insert 20W line string continuously
    /// </summary>
    [Fact]
    public void Log4netTest()
    {
      log4net.Repository.ILoggerRepository repository = log4net.LogManager.CreateRepository("NETCoreRepository");
      var fileInfo = new FileInfo("config/log4net.config");
      log4net.Config.XmlConfigurator.Configure(repository, fileInfo);
      log4net.Config.BasicConfigurator.Configure(repository);
      log4net.ILog log = log4net.LogManager.GetLogger(repository.Name, "NETCorelog4net");

      var total = 200000;
      var sw = new Stopwatch();
      sw.Start();
      for (int i = 0; i < total; i++)
      {
        log.Info("log4 bigdata test: " + i);
      }
      sw.Stop();
      log.Info($"total: {total}, Elapsed:{sw.ElapsedMilliseconds}");
      Output. Writeline ($"log4net test total: {total}, elapsed: {SW. Elapsedmilliseconds}");
    }

    /// <summary>
    ///Use NLog to insert 20W line string continuously
    /// </summary>
    [Fact]
    public void NlogTest()
    {

      NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
      var total = 200000;
      var sw = new Stopwatch();
      sw.Start();
      for (int i = 0; i < total; i++)
      {
        log.Info("nlog bigdata test: " + i);
      }
      sw.Stop();
      log.Info($"total: {total}, Elapsed:{sw.ElapsedMilliseconds}");
      Output. Writeline ($"NLog test total: {total}, elapsed: {SW. Elapsedmilliseconds}");
    }

  }
}

Test case 1: do not enable caching, insert 20W rows continuously

1.Log4net

To configure

log4net.config


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <!-- This section contains the log4net configuration settings -->
 <log4net>
  
  <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
   <file value="logfile/" />
   <appendToFile value="true" />
   <rollingStyle value="Composite" />
   <staticLogFileName value="false" />
   <datePattern value="yyyyMMdd'.log'" />
   <maxSizeRollBackups value="10" />
   <maximumFileSize value="1MB" />
   <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date | %message%newline" />
   </layout>
  </appender>

  <!-- Setup the root category, add the appenders and set the default level -->
  <root>
   <level value="ALL" />
   <appender-ref ref="RollingLogFileAppender" />
  </root>

 </log4net>
</configuration>

test result

Output log content:

2017-09-11 19:38:02,276 | log4 bigdata test: 0

2017-09-11 19:38:02,279 | log4 bigdata test: 1

… …

… …

2017-09-11 19:38:02,279 | log4 bigdata test: 199998

2017-09-11 19:38:02,279 | log4 bigdata test: 199999

Log4net time consumption:

Number of lines written: 200000, number of milliseconds: 7749

2.Nlog

To configure

nlog.config


<?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="Warn"
   internalLogFile="internal-nlog.txt">

 <!-- define various log targets -->
 <targets>
  <!-- write logs to file -->
  <target xsi:type="File" name="ownFile-web" fileName="logs/nlog-own-${shortdate}.log"
       layout="${longdate} | ${message}" 
      archiveAboveSize="1048576"/>
 </targets>

 <rules>
  <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
 </rules>
</nlog>

test result

Output log content:

2017-09-11 19:38:02,276 | nlog bigdata test: 0

2017-09-11 19:38:02,279 | nlog bigdata test: 1

……

……

2017-09-11 19:38:02,279 | nlog bigdata test: 199998

2017-09-11 19:38:02,279 | nlog bigdata test: 199999

NLog time consumption:

Number of lines written: 200000, number of milliseconds: 104468

Test case 2: enable buffer and insert 20W rows continuously

1.Log4net

Configure log4net.config


......

 <log4net>
  <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
   <bufferSize value="100" />

......

Time: log4net writes 200000 rows, 4672 milliseconds

2.Nlog

To configure

nlog.config


......

 <targets>
  <!-- write logs to file -->
  <default-wrapper xsi:type="BufferingWrapper" bufferSize="100"/>
  <target xsi:type="File" name="ownFile-web" fileName="logs/nlog-own-${shortdate}.log"
       layout="${longdate} | ${message}" 
      archiveAboveSize="1048576"/>
 </targets>

......

NLog time: 200000 rows written, 1605 milliseconds written

summary

Log component Edition Environmental Science Use case (enable buffer = 100) milliseconds (buffer not enabled) milliseconds
log4net 2.0.8 .netcore 2.0 20W line file write 4672 7749
nlog 5.0.0-beta10 .netcore 2.0 20W line file write 1605 104468

The code and the configuration file are on top. I don’t know the result of different configuration machines.

Will you prefer NLog or log4net No matter whether the buffer is enabled or not, the time consumption of log4net is relatively stable, within 10 seconds, the difference between before and after is 1.66 times; before and after the buffer is enabled, the time consumption difference of NLog is 65 times, and when the buffer is not enabled, it actually takes more than 100 seconds, although only 1.605 seconds after the buffer is enabled.

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.