欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解.Net core2.0日志组件Log4net、Nlog简单性能测试

程序员文章站 2022-06-24 23:21:29
.net core之log4net、nlog简单性能测试 比较log4net、nlog的文件写入性能(.netcore环境),涉及代码和配置如有不正确的地方,还请批评指正...

.net core之log4net、nlog简单性能测试

比较log4net、nlog的文件写入性能(.netcore环境),涉及代码和配置如有不正确的地方,还请批评指正。

测试环境

开发工具: vsual studio 2017 15.3

框架版本: .net core 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="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>

测试结果

输出日志内容:

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, 毫秒数:7749

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="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 7749
nlog 5.0.0-beta10 .netcore 2.0 20w行文件写入 1605 104468

代码和配置文件都在上边了,不知道不同配置的机器结果如何。

大家又会倾向于nlog还是log4net? log4net无论是否启用buffer,耗时都比较稳定,10秒以内,前后差距1.66倍;nlog在启用buffer前后,耗时差距65倍 ,未启用buffer竟然需要100多秒,虽然在启用buffer后仅1.605秒。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。