1.引用log4net.dll
2.在AssemblyInfo.cs中添加初始化:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
3.添加配置文件,内容如下:
public class log4netHelper { ////// 追加一条一卡通处理异常日志信息 /// /// 请求ID /// 异常 /// 附加信息 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; } ////// 追加一条一卡通请求日志信息 /// /// 请求ID /// 请求的IP地址 /// 时间 /// 请求时传入的参数 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; } ////// 追加一条一卡通处理结果日志 /// /// 请求ID /// 时间 /// 处理结果 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; } ////// 追加一条普通的日志信息 /// /// 日志内容 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,如需转载请自行联系原作者