demo地址:https://github.com/PuzzledAlien/log4net_demo 準備 log4net 最新版本是2.0.8 VS2017 .Net Core 2.2 測試電腦配置 windows10 64位 4核CPU 16G記憶體 log4net官網引導文檔 :http:// ...
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".read.log""/>
<!--日誌根據日期滾動-->
<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