首页 > 编程 > .NET > 正文

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

2024-07-10 12:48:46
字体:
来源:转载
供稿:网友

.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>            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表