.Netcore之日誌組件Log4net、Nlog性能比較

分類:IT技術 時間:2017-09-12

.Netcore之Log4net、Nlog性能比較

最近在寫一個開源.netcore web框架,需要一個高性能的日誌組件。目前要從log4net、nlog二選一,大家應該都有了解,就不贅述了。

此文目的是比較log4net、nlog的文件寫入性能(.netcore環境),涉及代碼和配置如有不正確的地方,還請批評指正。樓主已經和日誌杠上了。樓主是個喜歡寫代碼,不喜歡寫文檔的猿,多久才寫一篇文,真懶額!

切入正題

測試環境

開發工具: Vsual Studio 2017 15.3

框架版本: .netcore 2.0

操作系統:window10 Enterprise 1703

硬件配置:CPU I3-4170 3.7GHz,內存 8G,固態硬盤

日誌組件

鏈接log4net 2.0.8

鏈接nlog 5.0.0-beta10

測試用例

  1. 不啟用Buffer,連續插入20萬行字符串到文件,單文件最大1MB。

  2. 啟用Buffer為100,連續插入20萬行字符串到文件,單文件最大1MB。

測試方法

xunit單元測試。

測試代碼


using system;
using System.Diagnostics;
using System.IO;
using Xunit;
using Xunit.Abstractions;

namespace Demo.Logging.Tests
{
    /// <summary>
    /// Log4net、Nlog日誌文件寫入對比
    /// </summary>
    public class BigDataTest
    {
        private readonly ITestOutputHelper output;
        public BigDataTest(ITestOutputHelper outputHelper)
        {
            output = outputHelper;
        }

        /// <summary>
        /// 使用Log4net連續插入20W行字符串
        /// </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測試 total: {total}, Elapsed:{sw.ElapsedMilliseconds}");
        }

        /// <summary>
        /// 使用Nlog連續插入20W行字符串
        /// </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測試 total: {total}, Elapsed:{sw.ElapsedMilliseconds}");
        }

    }
}

測試用例一:不啟用緩存,連續插入20W行

1.Log4net

配置

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=http://www.cnblogs.com/supernebula/p/"logfile/" />
      <appendToFile value=http://www.cnblogs.com/supernebula/p/"true" />
      <rollingStyle value=http://www.cnblogs.com/supernebula/p/"Composite" />
      <staticLogFileName value=http://www.cnblogs.com/supernebula/p/"false" />
      <datePattern value=http://www.cnblogs.com/supernebula/p/"yyyyMMdd'.log'" />
      <maxSizeRollBackups value=http://www.cnblogs.com/supernebula/p/"10" />
      <maximumFileSize value=http://www.cnblogs.com/supernebula/p/"1MB" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value=http://www.cnblogs.com/supernebula/p/"%date | %message%newline" />
      </layout>
    </appender>

    <!-- Setup the root category, add the appenders and set the default level -->
    <root>
      <level value=http://www.cnblogs.com/supernebula/p/"ALL" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>

  </log4net>
</configuration>

測試結果

輸出日誌內容:

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耗時:

寫入行數:200000, 毫秒數:17749 (更正)

2.Nlog

配置

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>

測試結果

輸出日誌內容:

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耗時:

寫入行數:200000, 毫秒數:104468

測試用例二:啟用Buffer,連續插入20W行

1.Log4net

配置 log4net.config


......

  <log4net>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <bufferSize value=http://www.cnblogs.com/supernebula/p/"100" />

......

耗時:

Log4net寫入行數:200000, 毫秒數:4672

2.Nlog

配置

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耗時:

寫入行數:200000, 毫秒數:1605

總結

日誌組件 版本 環境 用例 (啟用Buffer=100)毫秒數 (不啟用Buffer)毫秒數 log4net 2.0.8 .netcore 2.0 20W行文件寫入 4672 17749 (更正) nlog 5.0.0-beta10 .netcore 2.0 20W行文件寫入 1605 104468

代碼和配置文件都在上邊了,不知道不同配置的機器結果如何。

轉載請註明出處http://www.cnblogs.com/supernebula/p/7506993.html

Tags: 文件 測試 using 字符串 日誌 Log4net

文章來源:


ads
ads

相關文章
ads

相關文章

ad