标签:
log4net.dll是apache发布的用来记录log的dll文件
这里举个例子相信大家就知道怎么用了,新建一个console项目,添加log4net.dll后再添加应用程序配置文件
1 <?xml version="1.0"?> 2 <configuration> 3 <configSections> 4 <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/> 5 </configSections> 6 <log4net> 7 <!--定义输出到文件中--> 8 <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> 9 <!--定义文件存放位置--> 10 <file value="log\\"/> 11 <appendToFile value="true"/> 12 <rollingStyle value="Date"/> 13 <datePattern value="yyyy\\yyyyMM\\yyyyMMdd‘.txt‘"/> 14 <staticLogFileName value="false"/> 15 <param name="MaxSizeRollBackups" value="100"/> 16 <layout type="log4net.Layout.PatternLayout"> 17 <!--每条日志末尾的文字说明--> 18 <!--输出格式--> 19 <!--样例:2008-03-26 13:42:32,111 [10] INFO Log4NetDemo.MainClass [(null)] - info--> 20 <conversionPattern value="%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别: %-5level %n出错类:%logger property: [%property{NDC}] - %n错误描述:%message%newline %n"/> 21 </layout> 22 </appender> 23 <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> 24 <layout type="log4net.Layout.PatternLayout"> 25 <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/> 26 </layout> 27 </appender> 28 <root> 29 <!--文件形式记录日志--> 30 <appender-ref ref="RollingLogFileAppender"/> 31 <appender-ref ref="ConsoleAppender"/> 32 </root> 33 </log4net> 34 </configuration>
关于这个配置文件更加详细的解释可看http://blog.csdn.net/zhoufoxcn/article/details/6029021
Program.cs:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using log4net; 6 using System.Reflection; 7 8 namespace Log4NetDemo 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); 15 16 //LogHelper.WriteLog(typeof(Program), "hello world!!"); 17 log.Error("error", new Exception("发生一个异常")); 18 log.Fatal("fatal", new Exception("发生了一个致命错误")); 19 log.Info("info"); 20 log.Debug("debug"); 21 log.Warn("warn"); 22 Console.WriteLine("日志记录完毕"); 23 Console.ReadLine(); 24 } 25 } 26 }
标签:
原文地址:http://www.cnblogs.com/yingzhongwen/p/4899415.html