前言
经验表明,日志记录往往是软件开发周期中的重要组成部分。它具有以下几个优点:它可以提供应用程序运行时的精确环境,可供开发人员尽快找到应用程序中的Bug;一旦在程序中加入了Log 输出代码,程序运行过程中就能生成并输出日志信息而无需人工干预。另外,日志信息可以输出到不同的地方(控制台,文件等)以备以后研究之用。
Log4net就是为这样一个目的设计的,用于.NET开发环境的日志记录包。
第一步、下载log4net动态链接库
log4net官方网站:http://logging.apache.org/log4net/
如下图所示针对不同版本的.net framework有不同版本的log4net.dll与之相对应。
第三步、使用
1、在Visual Studio开发项目中引用对应.net framework相应版本的log4net.dll文件
2、在App.config或web.config配置文件中做相应配置
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <appSettings> <add key="Test" value="True"/> </appSettings> <log4net> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <!--日志路径(当前设置为应用程序目录下log目录下)--> <param name= "File" value= "log\"/> <!--是否是向文件中追加日志--> <param name= "AppendToFile" value= "true"/> <!--log保留天数--> <param name= "MaxSizeRollBackups" value= "10"/> <!--日志文件名是否是固定不变的--> <param name= "StaticLogFileName" value= "false"/> <!--日志文件名格式为:2008-08-31.log--> <param name= "DatePattern" value= "yyyy-MM-dd".log""/> <!--日志根据日期滚动--> <param name= "RollingStyle" value= "Date"/> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n %loggername" /> </layout> </appender> <!-- 控制台前台显示日志 --> <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender"> <mapping> <level value="ERROR" /> <foreColor value="Red, HighIntensity" /> </mapping> <mapping> <level value="Info" /> <foreColor value="Green" /> </mapping> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%n%date{HH:mm:ss,fff} [%-5level] %m" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="Info" /> <param name="LevelMax" value="Fatal" /> </filter> </appender> <root> <!--(高) OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL (低) --> <level value="all" /> <appender-ref ref="ColoredConsoleAppender"/> <appender-ref ref="RollingLogFileAppender"/> </root> </log4net> </configuration>
3、添加LogHelper类,LogHelper类代码如下
***命名空间前加上[assembly: log4net.Config.XmlConfigurator(Watch = true)]至关重要
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 6 [assembly: log4net.Config.XmlConfigurator(Watch = true)]//这一句很重要 7 namespace WebApplication1 8 { 9 public class LogHelper 10 { 11 12 /// <summary> 13 /// 添加异常日志 14 /// </summary> 15 /// <param name="t">发生异常主体</param> 16 /// <param name="ex">异常对象</param> 17 public static void AddErrorLog(Type t, Exception ex) 18 { 19 log4net.ILog log = log4net.LogManager.GetLogger(t); 20 log.Error("MyError", ex); 21 } 22 23 /// <summary> 24 /// 添加异常日志 25 /// </summary> 26 /// <param name="t">发生异常主体</param> 27 /// <param name="msg">异常信息</param> 28 public static void AddErrorLog(Type t, string msg) 29 { 30 log4net.ILog log = log4net.LogManager.GetLogger(t); 31 log.Error(msg); 32 } 33 34 /// <summary> 35 /// 添加信息日志 36 /// </summary> 37 /// <param name="t"></param> 38 /// <param name="ex"></param> 39 public static void AddInfoLog(Type t, Exception ex) 40 { 41 log4net.ILog log = log4net.LogManager.GetLogger(t); 42 log.Info("MyInfo", ex); 43 } 44 45 /// <summary> 46 /// 添加信息日志 47 /// </summary> 48 /// <param name="t"></param> 49 /// <param name="msg"></param> 50 public static void AddInfoLog(Type t, string msg) 51 { 52 log4net.ILog log = log4net.LogManager.GetLogger(t); 53 log.Info(msg); 54 } 55 56 /// <summary> 57 /// 添加警告日志 58 /// </summary> 59 /// <param name="t"></param> 60 /// <param name="ex"></param> 61 public static void AddWarnLog(Type t, Exception ex) 62 { 63 log4net.ILog log = log4net.LogManager.GetLogger(t); 64 log.Warn("MyWarn", ex); 65 } 66 67 public static void AddWarnLog(Type t, string msg) 68 { 69 log4net.ILog log = log4net.LogManager.GetLogger(t); 70 log.Warn(msg); 71 } 72 73 /// <summary> 74 /// 添加致命错误日志 75 /// </summary> 76 /// <param name="t"></param> 77 /// <param name="ex"></param> 78 public static void AddFatalLog(Type t, Exception ex) 79 { 80 log4net.ILog log = log4net.LogManager.GetLogger(t); 81 log.Fatal("MyFatal", ex); 82 } 83 84 public static void AddFatalLog(Type t, string msg) 85 { 86 log4net.ILog log = log4net.LogManager.GetLogger(t); 87 log.Fatal(msg); 88 } 89 } 90 }
4、掉用LogHelper添加日志
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using log4net.Config; namespace log4netDemo_3._5 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { LogHelper.AddInfoLog(typeof(Form1), "Test "); int i = 0; int j = 10; try { var k = j / i; } catch (Exception ex) { LogHelper.AddErrorLog(typeof(Form1), ex); } } } }