Net core2.0 Log Components Log4net Nlog Simple Performance Test
- 2021-10-25 06:21:21
- OfStack
Simple performance test of Log4net and Nlog of Net core
Compare the file writing performance of log4net and nlog (. netcore environment). If there are any incorrect codes and configurations, please criticize and correct them.
Test environment
Development Tools: Vsual Studio 2017 15.3
Framework version:. net core 2.0
Operating system: window10 Enterprise 1703
Hardware configuration: CPU I3-4170 3.7 GHz, memory 8G, solid state disk
Log component
log4net 2.0.8
nlog 5.0.0-beta10
Test case
1. Do not enable Buffer, insert 200,000 lines of strings into files continuously, with a maximum of 1MB for a single file.
2. Enable Buffer to 100, insert 200, 000 lines of string into file continuously, single file maximum 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>
/// Log4net , Nlog Log file write comparison
/// </summary>
public class BigDataTest
{
private readonly ITestOutputHelper output;
public BigDataTest(ITestOutputHelper outputHelper)
{
output = outputHelper;
}
/// <summary>
/// Use Log4net Continuous insertion 20W Line string
/// </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 Continuous insertion 20W Line string
/// </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: Insert 20W rows continuously without enabling caching
1.Log4net
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 results
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 consuming:
Number of lines written: 200,000, number of milliseconds: 7749
2.Nlog
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 results
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 consuming:
Number of lines written: 200,000, number of milliseconds: 104,468
Test Case 2: Enable Buffer and insert 20W rows in succession
1.Log4net
Configuring log4net. config
......
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<bufferSize value="100" />
......
Time spent: Log4net rows written: 200,000, milliseconds: 4672
2.Nlog
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 spent: Rows written: 200,000, milliseconds: 1605
Summarize
日志组件 | 版本 | 环境 | 用例 | (启用Buffer=100)毫秒数 | (不启用Buffer)毫秒数 |
---|---|---|---|---|---|
log4net | 2.0.8 | .netcore 2.0 | 20W行文件写入 | 4672 | 7749 |
nlog | 5.0.0-beta10 | .netcore 2.0 | 20W行文件写入 | 1605 | 104468 |
The code and configuration file are all on it, so I don't know how the machines with different configurations will work out.
Will people prefer nlog or log4net? Whether log4net is enabled or not, the time consumption is relatively stable, within 10 seconds, and the difference between before and after is 1.66 times; nlog takes 65 times before and after buffer is enabled, and it takes more than 100 seconds without buffer, although it only takes 1.605 seconds after buffer is enabled.