.Netcore之日志组件Log4net、Nlog性能比较

.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
{
    /// 
    /// Log4net、Nlog日志文件写入对比
    /// 
    public class BigDataTest
    {
        private readonly ITestOutputHelper output;
        public BigDataTest(ITestOutputHelper outputHelper)
        {
            output = outputHelper;
        }

        /// 
        /// 使用Log4net连续插入20W行字符串
        /// 
        [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}");
        }

        /// 
        /// 使用Nlog连续插入20W行字符串
        /// 
        [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


 version="1.0" encoding="utf-8" ?>

  
  
    
     name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
       value="logfile/" />
       value="true" />
       value="Composite" />
       value="false" />
       value="yyyyMMdd'.log'" />
       value="10" />
       value="1MB" />
       type="log4net.Layout.PatternLayout">
         value="%date | %message%newline" />
      
    

    
    
       value="ALL" />
       ref="RollingLogFileAppender" />
    

  

测试结果

输出日志内容:

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


 version="1.0" encoding="utf-8" ?>
 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">

  
  
    
     xsi:type="File" name="ownFile-web" fileName="logs/nlog-own-${shortdate}.log"
              layout="${longdate} | ${message}" 
            archiveAboveSize="1048576"/>
  

  
     name="*" minlevel="Trace" writeTo="ownFile-web" />
  

测试结果

输出日志内容:

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


......

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

......

耗时:

Log4net写入行数:200000, 毫秒数:4672

2.Nlog

配置

nlog.config


......

  
    
     xsi:type="BufferingWrapper" bufferSize="100"/>
     xsi:type="File" name="ownFile-web" fileName="logs/nlog-own-${shortdate}.log"
              layout="${longdate} | ${message}" 
            archiveAboveSize="1048576"/>
  

......

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

你可能感兴趣的:(.Netcore之日志组件Log4net、Nlog性能比较)