log4net 使用步骤
1.安装log4net
官网首页:http://logging.apache.org/log4net/
下载地址:http://logging.apache.org/log4net/download_log4net.cgi
找到对应.net版本的 log4net.dll
在项目中引用 下载的 log4net.dll
3.书写配置文件:
log4net可以将日志写到控制台,文本文件,数据库(access,MS SQL,mysql,oracle,SQLite 等...)所以需要填写配置文件
配置文件的写法:http://logging.apache.org/log4net/release/config-examples.html
第一步:添加节点 在configSections添加 section log4net
<configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections>
第二步:在configuration节点下添加<log4net>
<log4net> <!-- Console部分log输出格式的设定 --> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger %ndc - %message%newline" /> </layout> </appender> <!-- 日志文件部分log输出格式的设定 --> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="log\\" /> <appendToFile value="true" /> <maxSizeRollBackups value="10" /> <maximumFileSize value="10MB" /> <rollingStyle value="Size" /> <staticLogFileName value="true" /> <layout type="log4net.Layout.PatternLayout"> <header value="[Header] " /> <footer value="[Footer] " /> <ConversionPattern value="%date [%thread] %-5level - %message%newline%newline" /> </layout> </appender> <!-- Setup the root category, add the appenders and set the default level --> <root> <level value="ALL" /> <appender-ref ref="ConsoleAppender" /> <appender-ref ref="RollingLogFileAppender" /> </root> </log4net>
完整的APP.config 如下:
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup> <log4net> <!-- Console部分log输出格式的设定 --> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger %ndc - %message%newline" /> </layout> </appender> <!-- 日志文件部分log输出格式的设定 --> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="log\\" /> <appendToFile value="true" /> <maxSizeRollBackups value="10" /> <maximumFileSize value="10MB" /> <rollingStyle value="Size" /> <staticLogFileName value="true" /> <layout type="log4net.Layout.PatternLayout"> <header value="[Header] " /> <footer value="[Footer] " /> <ConversionPattern value="%date [%thread] %-5level - %message%newline%newline" /> </layout> </appender> <!-- Setup the root category, add the appenders and set the default level --> <root> <level value="ALL" /> <appender-ref ref="ConsoleAppender" /> <appender-ref ref="RollingLogFileAppender" /> </root> </log4net> <appSettings> <add key="RegistrationCode" value=""/> </appSettings> </configuration>
4.添加一个LogHelper类,以便随时静态调用
using System; using System.Reflection; using log4net; namespace Text2Excel.Helper { public static class LogHelper { private static readonly ILog mLog = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); public static void WriteLog(string date, Exception e) { mLog.Error(date + ":" + e.Message + "\r\n" + e.StackTrace); } public static void WriteLog(Exception ex) { mLog.Error(ex.Message + "\r\n" + ex.StackTrace); } public static void WriteLog(string msg) { mLog.Error(msg); } //TODO: 扩展自己想用的方法 } }