标签:
1.引用log4net.dll
2.在AssemblyInfo.cs中添加初始化:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "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>
4.添加Log4netHelper类:
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; } }
转自:http://www.cnblogs.com/youring2/archive/2011/04/27/2030424.html
标签:
原文地址:http://www.cnblogs.com/anbylau2130/p/4942271.html