內容轉載自:https://www.cnblogs.com/youring2/archive/2011/04/27/2030424.htmlhtml
1.引用log4net.dllapp
2.在AssemblyInfo.cs中添加初始化:post
[assembly: log4net.Config.XmlConfigurator(ConfigFile
= "log4net.config", Watch = true)]
= "log4net.config", Watch = true)]
3.添加配置文件,內容以下:日誌
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net>
<root>
</root>
<logger name="OneCardRequest">
<level value="INFO" />
<appender-ref ref="rollingRequestFile" />
</logger>
<logger name="OneCardResult">
<level value="INFO" />
<appender-ref ref="rollingResultFile" />
</logger>
<logger name="OneCardException">
<level value="ERROR" />
<appender-ref ref="rollingExceptionFile" />
</logger>
<logger name="InfoLog">
<level value="ALL" />
<appender-ref ref="rollingInfoFile" />
</logger>
<appender name="rollingRequestFile" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" type="" value="Log/Request/" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyyMMdd" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%n[%d] %c %n%m%n%n" />
<param name="Header" value="-----------BEGIN-----------" />
<param name="Footer" value="------------END------------" />
</layout>
</appender>
<appender name="rollingResultFile" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" type="" value="Log/Result/" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyyMMdd" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%n[%d] %c %n%m%n%n" />
<param name="Header" value="-----------BEGIN-----------" />
<param name="Footer" value="------------END------------" />
</layout>
</appender>
<appender name="rollingExceptionFile" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" type="" value="Log/Exception/" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyyMMdd" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%n[%d] %c %n%m%n%n" />
<param name="Header" value="-----------BEGIN-----------" />
<param name="Footer" value="------------END------------" />
</layout>
</appender>
<appender name="rollingInfoFile" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" type="" value="Log/Info/" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyyMMdd" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%n[%d] %c %n%m%n%n" />
<param name="Header" value="-----------BEGIN-----------" />
<param name="Footer" value="------------END------------" />
</layout>
</appender>
</log4net>
</configuration>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net>
<root>
</root>
<logger name="OneCardRequest">
<level value="INFO" />
<appender-ref ref="rollingRequestFile" />
</logger>
<logger name="OneCardResult">
<level value="INFO" />
<appender-ref ref="rollingResultFile" />
</logger>
<logger name="OneCardException">
<level value="ERROR" />
<appender-ref ref="rollingExceptionFile" />
</logger>
<logger name="InfoLog">
<level value="ALL" />
<appender-ref ref="rollingInfoFile" />
</logger>
<appender name="rollingRequestFile" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" type="" value="Log/Request/" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyyMMdd" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%n[%d] %c %n%m%n%n" />
<param name="Header" value="-----------BEGIN-----------" />
<param name="Footer" value="------------END------------" />
</layout>
</appender>
<appender name="rollingResultFile" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" type="" value="Log/Result/" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyyMMdd" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%n[%d] %c %n%m%n%n" />
<param name="Header" value="-----------BEGIN-----------" />
<param name="Footer" value="------------END------------" />
</layout>
</appender>
<appender name="rollingExceptionFile" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" type="" value="Log/Exception/" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyyMMdd" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%n[%d] %c %n%m%n%n" />
<param name="Header" value="-----------BEGIN-----------" />
<param name="Footer" value="------------END------------" />
</layout>
</appender>
<appender name="rollingInfoFile" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" type="" value="Log/Info/" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyyMMdd" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%n[%d] %c %n%m%n%n" />
<param name="Header" value="-----------BEGIN-----------" />
<param name="Footer" value="------------END------------" />
</layout>
</appender>
</log4net>
</configuration>
4.添加Log4netHelper類:code
public class log4netHelper { /// <summary> /// 追加一條一卡通處理異常日誌信息 /// </summary> /// <param name="id">請求ID</param> /// <param name="ex">異常</param> /// <param name="attachMessage">附加信息</param> public static void Exception(string id, Exception ex, string attachMessage) { log4net.ILog log = log4net.LogManager.GetLogger("OneCardException"); if (log.IsErrorEnabled) { string strMessage = string.Format("RequestID:{0}\r\nMesssage:{1}\r\nStackTrace:{2}\r\nAttachMessage:{3}", id, ex.Message, ex.StackTrace, attachMessage); log.Error(strMessage); } log = null; } /// <summary> /// 追加一條一卡通請求日誌信息 /// </summary> /// <param name="id">請求ID</param> /// <param name="ip">請求的IP地址</param> /// <param name="datetime">時間</param> /// <param name="strParam">請求時傳入的參數</param> public static void RequestLog(string id, string ip, string datetime, string strParam) { log4net.ILog log = log4net.LogManager.GetLogger("OneCardRequest"); if (log.IsInfoEnabled) { string strMessage = string.Format("RequestID:{0}\r\nIP:{1}\r\nDateTime:{2}\r\nParam:{3}", id, ip, datetime, strParam); log.Info(strMessage); } log = null; } /// <summary> /// 追加一條一卡通處理結果日誌 /// </summary> /// <param name="id">請求ID</param> /// <param name="datetime">時間</param> /// <param name="strResult">處理結果</param> public static void ResultLog(string id, string datetime, string strResult) { log4net.ILog log = log4net.LogManager.GetLogger("OneCardResult"); if (log.IsInfoEnabled) { string strMessage = string.Format("RequestID:{0}\r\nDateTime:{1}\r\nParam:{2}", id, datetime, strResult); log.Info(strMessage); } log = null; } /// <summary> /// 追加一條普通的日誌信息 /// </summary> /// <param name="message">日誌內容</param> public static void Info(string message) { log4net.ILog log = log4net.LogManager.GetLogger("InfoLog"); if (log.IsInfoEnabled) { log.Info(message); } log = null; } }