.Net Core 实践 - 使用log4net记录日志(1)
程序员文章站
2022-04-08 21:40:16
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官网引导文档 :
配置和代码
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