标签:
1 <configuration> 2 <configSections> 3 <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> 4 </configSections> 5 <log4net> 6 <!--定义输出到文件中--> 7 <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> 8 <!--定义文件存放位置--> 9 <file value="log\\"/> 10 <appendToFile value="true"/> 11 <rollingStyle value="Date"/> 12 <datePattern value="yyyy\\yyyyMM\\yyyyMMdd‘.txt‘"/> 13 <staticLogFileName value="false"/> 14 <param name="MaxSizeRollBackups" value="100"/> 15 <layout type="log4net.Layout.PatternLayout"> 16 <!--每条日志末尾的文字说明--> 17 <!--输出格式--> 18 <!--样例:2008-03-26 13:42:32,111 [10] INFO Log4NetDemo.MainClass [(null)] - info--> 19 <conversionPattern value="%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别: %-5level %n出错类:%logger property: [%property{NDC}] - %n错误描述:%message%newline %n"/> 20 </layout> 21 </appender> 22 <root> 23 <level value="ERROR"/> 24 <!--文件形式记录日志--> 25 <appender-ref ref="RollingLogFileAppender"/> 26 </root> 27 </log4net> 28 </configuration>
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 [assembly: log4net.Config.XmlConfigurator(Watch = true)] 7 namespace TestLog4Net 8 { 9 public class LogHelper 10 { 11 /// <summary> 12 /// 输出日志到Log4Net 13 /// </summary> 14 /// <param name="t"></param> 15 /// <param name="ex"></param> 16 #region static void WriteLog(Type t, Exception ex) 17 public static void WriteLog(Type t, Exception ex) 18 { 19 log4net.ILog log = log4net.LogManager.GetLogger(t); 20 log.Error("Error", ex); 21 } 22 #endregion 23 24 /// <summary> 25 /// 输出日志到Log4Net 26 /// </summary> 27 /// <param name="t"></param> 28 /// <param name="msg"></param> 29 #region static void WriteLog(Type t, string msg) 30 public static void WriteLog(Type t, string msg) 31 { 32 log4net.ILog log = log4net.LogManager.GetLogger(t); 33 log.Error(msg); 34 } 35 #endregion 36 } 37 }
// HOW TO USE LogHelper.WriteLog(typeof(WebForm1), "test");
标签:
原文地址:http://www.cnblogs.com/Czhipu/p/4829054.html