一:下载log4net的程序集 下载地址:
http://logging.apache.org/log4net/
使用情况:
二、配置文件设置 Config配置文件log4net.xml
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <log4net> <root name="Logger"> <level value="ALL" /> <appender-ref ref="TraceLog"/> <appender-ref ref="WarnLog" /> <appender-ref ref="ErrorLog" /> </root> <appender name="TraceLog" type="log4net.Appender.RollingFileAppender,log4net" > <file value="LogTrace" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyy-MM-dd".LOG"" /> <staticLogFileName value="false" /> <layout type="log4net.Layout.PatternLayout,log4net"> <conversionPattern value="%newline %n记录时间:%d %n线程ID:[%thread] %n日志级别: %-5level %n描述:%message %n ---------------------------------------------------------------%newline"/> <footer value="---------------------------------------------------------------"/> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="INFO" /> <levelMax value="INFO" /> </filter> </appender> <appender name="WarnLog" type="log4net.Appender.RollingFileAppender,log4net" > <file value="LogWarn" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyy-MM-dd".LOG"" /> <staticLogFileName value="false" /> <layout type="log4net.Layout.PatternLayout,log4net"> <conversionPattern value="%newline %n记录时间:%d %n线程ID:[%thread] %n日志级别: %-5level%n警告描述:%message %n异常信息:%n%exception %n ---------------------------------------------------------------%newline"/> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="WARN" /> <levelMax value="WARN" /> </filter> </appender> <appender name="ErrorLog" type="log4net.Appender.RollingFileAppender,log4net" > <file value="LogError" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyy-MM-dd".LOG"" /> <staticLogFileName value="false" /> <layout type="log4net.Layout.PatternLayout,log4net"> <conversionPattern value="%newline %n记录时间:%d %n线程ID:[%thread] %n日志级别: %-5level %n错误描述:%message %n异常信息:%n%exception %n ---------------------------------------------------------------%newline"/> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="ERROR" /> <levelMax value="ERROR" /> </filter> </appender> </log4net> </configuration>
三、Logger工具类 :
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; namespace MvcApplication.Models { public class Logger { private static readonly log4net.ILog log = log4net.LogManager.GetLogger("Logger"); static Logger() { string path = string.Format(@"{0}Configlog4net.xml", AppDomain.CurrentDomain.BaseDirectory); if (File.Exists(path)) { log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(path)); } } /// <summary> /// 记录普通业务日志 /// </summary> /// <param name="info"></param> public static void Info(object info) { if (log.IsInfoEnabled) { log.Info(info); } } /// <summary> /// 记录警告日志 /// </summary> /// <param name="warn"></param> public static void Warn(object warn) { if (log.IsWarnEnabled) { log.Warn(warn); } } /// <summary> /// 记录警告日志 /// </summary> /// <param name="warn"></param> public static void Warn(object warn, Exception ex) { if (log.IsWarnEnabled) { log.Warn(warn, ex); } } /// <summary> /// 记录异常日志 /// </summary> /// <param name="error"></param> public static void Error(object error) { if (log.IsErrorEnabled) { log.Error(error); } } /// <summary> /// 记录异常日志 /// </summary> /// <param name="error"></param> /// <param name="ex"></param> public static void Error(object error, Exception ex) { if (log.IsWarnEnabled) { log.Error(error, ex); } } } }
四、测试代码:(mvc web测试)
public ActionResult Index() { Logger.Info("log4net日志测试!!!!"); try { string a = null; int len = a.Length; } catch (Exception ex) { Logger.Error("程序异常", ex); } return View(); }
五、查看日志: