log4net小结
寫在前面的:
在網(wǎng)上找的,再加上自己的心得
AssemblyInfo.cs:(加載配置文件也可以寫到app.config中)
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "systemconfig/log4net.xml", Watch = true)]
Test.cs:
ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
log4net.xml(注意:要將此文件的屬性設(shè)置為 如有較新的則復(fù)制):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
? <configSections>
??? <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"??? />
? </configSections>
? <log4net>
??? <root>
????? <level value="all" />
????? <appender-ref ref="RollingLogFileAppender" />
????? <appender-ref ref="ConsoleAppender" />
??? </root>
??? <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net" >
????? <file value="./logs/log.log" />
????? <appendToFile value="true" />
????? <rollingStyle value="Composite" />
????? <datePattern value="yyyy-MM-dd".log"" />
????? <!--設(shè)置無限備份=-1 ,最大備份數(shù)為50-->
????? <maxSizeRollBackups value="50"/>
????? <!--每個(gè)文件的最大20k-->
????? <maximumFileSize value="20"/>
????? <!--
????? <maximumFileSize value="50MB" />
????? -->
????? <!--名稱是否可以更改:
?????????? false為可以更改-->
????? <param name="StaticLogFileName" value="false" />
?????
????? <layout type="log4net.Layout.PatternLayout">
??????? <param name="ConversionPattern" value="%d [%t] %-2p %c %method %L- %m%n"/>
????? </layout>
????? <filter type="log4net.Filter.LevelRangeFilter">
??????? <param name="LevelMin" value="DEBUG" />
??????? <param name="LevelMax" value="FATAL" />
????? </filter>
??? </appender>
??? <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
????? <layout type="log4net.Layout.PatternLayout">
??????? <param name="ConversionPattern" value="%d [%t] %-5p %c [%L] - %m%n" />
????? </layout>
??? </appender>
? </log4net>
補(bǔ)充:
Log4net中ConversionPattern的解釋說明:http://www.cnblogs.com/calaio/archive/2006/05/29/412024.html
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value=" -----------------------header-------------------------- " />
<param name="Footer" value="-----------------------footer-------------------------- " />
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] %X{auth} - Line:%L %m%n" />
</layout>
%m(message):輸出的日志消息,如ILog.Debug(…)輸出的一條消息
%n(new line):換行
%d(datetime):輸出當(dāng)前語句運(yùn)行的時(shí)刻
%r(run time):輸出程序從運(yùn)行到執(zhí)行到當(dāng)前語句時(shí)消耗的毫秒數(shù)
%t(thread id):當(dāng)前語句所在的線程ID
%p(priority): 日志的當(dāng)前優(yōu)先級(jí)別,即DEBUG、INFO、WARN…等
%c(class):當(dāng)前日志對(duì)象的名稱
%L:輸出語句所在的行號(hào)
%F:輸出語句所在的文件名
%-數(shù)字:表示該項(xiàng)的最小長(zhǎng)度,如果不夠,則用空格填充
總結(jié)
- 上一篇: hdu1695
- 下一篇: 19、SQL Server 数据修改之I