Log4Net使用
log4net是一个强大的日志撰写DLL。适用于不同的framework版本。
本文适用于那些不考虑使用原理,直接使用的人。
详细使用可以参考周公的博客:
1、log4net使用方法 http://blog.csdn.net/zhoufoxcn/article/details/2220533
2、log4net使用详情 http://blog.csdn.net/zhoufoxcn/article/details/6029021
使用详情:
一、引用framework版本规定下的DLL
二、配置 配置文件 log4net.config
三、引用配置文件[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
四、开始使用声明 该变量 ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
使用样例:
配置文件 样例:
配置文件名称:log4net.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <log4net> <!-- This writes the log information to the console window. It only logs events that are at least at the INFO level (which would mean that DEBUG events are not captured. --> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date{ABSOLUTE} [%thread] %level %logger - %message%newline%exception"/> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="INFO" /> <levelMax value="FATAL" /> </filter> </appender> <appender name="RollingFileAppenderSize" type="log4net.Appender.RollingFileAppender"> <file value="logs/log.txt" /> <appendToFile value="true" /> <rollingStyle value="Size" /> <maxSizeRollBackups value="10" /> <maximumFileSize value="100KB" /> <staticLogFileName value="true" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger - %message%newline%exception" /> </layout> </appender> <appender name="RollingLogFileAppenderDate" type="log4net.Appender.RollingFileAppender"> <file value="logs/logfile.log" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyy-MM-dd" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger - %message%newline%exception" /> </layout> </appender> <!-- This stores information in the mylogfile.txt file. It only captures log events that contain the key word test or error. <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="updatesatus.log" /> <appendToFile value="true" /> <rollingStyle value="Size" /> <maxSizeRollBackups value="5" /> <maximumFileSize value="10MB" /> <staticLogFileName value="true" /> <filter type="log4net.Filter.StringMatchFilter"> <stringToMatch value="test" /> </filter> <filter type="log4net.Filter.StringMatchFilter"> <stringToMatch value="error" /> </filter> <filter type="log4net.Filter.DenyAllFilter" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %level %logger - %message%newline%exception" /> </layout> </appender> --> <root> <level value="INFO"/> <appender-ref ref="RollingLogFileAppenderDate"/> <appender-ref ref="ConsoleAppender" /> </root> </log4net> </configuration>