.Net Core 实践 - 使用log4net记录日志(1)

demo地址:https://github.com/PuzzledAlien/log4net_demo

准备

  • log4net 最新版本是2.0.8
  • VS2017
  • .Net Core 2.2
  • 测试电脑配置 windows10 64位 4核CPU 16G内存

log4net官网引导文档 :http://logging.apache.org/log4net/release/manual/introduction.html

配置和代码

log4net.config配置如下

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<!--日志路径-->
<param name= "File" value= ".\App_Log\servicelog\"/>
<!--是否是向文件中追加日志-->
<param name= "AppendToFile" value= "true"/>
<!--log保留天数-->
<param name= "MaxSizeRollBackups" value= "10"/>
<!--每个log文件最大是2M,如果超过2M将重新创建一个新的log文件,并将原来的log文件备份。-->
<maximumFileSize value="2MB" />
<!--日志文件名是否是固定不变的-->
<param name= "StaticLogFileName" value= "false"/>
<!--日志文件名格式为:2008-08-31-12.log-->
<param name= "DatePattern" value= "yyyy-MM-dd-HH-mm&quot;.read.log&quot;"/>
<!--日志根据日期滚动-->
<param name= "RollingStyle" value= "Date"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n %loggername" />
</layout>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>
<!-- 控制台前台显示日志 -->
<appender name="ManagedColoredConsoleAppender" type="log4net.Appender.ManagedColoredConsoleAppender">
<mapping>
<level value="ERROR" />
<foreColor value="Red" />
</mapping>
<mapping>
<level value="Info" />
<foreColor value="Green" />
</mapping>
<mapping>
<level value="DEBUG" />
<foreColor value="Blue" />
</mapping>
<mapping>
<level value="WARN" />
<foreColor value="Yellow" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%n%date{HH:mm:ss,fff} [%-5level] %m" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="Fatal" />
</filter>
</appender>
<root>
<!--(高) OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL (低) -->
<level value="ALL" />
<appender-ref ref="ManagedColoredConsoleAppender"/>
<appender-ref ref="RollingLogFileAppender"/>
</root>
</log4net>
</configuration>

添加引用

using log4net;
using log4net.Config;
using log4net.Repository;

代码如下:

        private static ILoggerRepository _loggerRepository;
static void Main(string[] args)
{
_loggerRepository = LogManager.CreateRepository("ConsoleApp3");
XmlConfigurator.ConfigureAndWatch(_loggerRepository, new FileInfo("log4net.config"));
var log = LogManager.GetLogger(_loggerRepository.Name, typeof(Program)); const int numberOfCycles = 20000; var sw = Stopwatch.StartNew();
for (var i = 0; i < numberOfCycles; i++)
{
log.InfoFormat("testNum: {0} ", i);
}
sw.Stop(); Console.WriteLine();
Console.WriteLine("Ellapsed: {0}, numPerSec: {1}", sw.ElapsedMilliseconds, numberOfCycles / (sw.ElapsedMilliseconds / (double)1000)); Console.WriteLine("Hello World!"); Console.ReadKey();
}

执行结果:

本地写log文本日志和Console打印日志的时间14720毫秒 一秒钟写日志1359条

Ellapsed: 14720, numPerSec: 1358.69565217391
上一篇:json.decoder.JSONDecodeError: Unexpected UTF-8 BOM (decode using utf-8-sig): line 1 column 1


下一篇:.net core实践系列之短信服务-Sikiro.SMS.Api服务的实现