1.新建一个空的ASP.Net 空项目,然后添加Default.aspx窗体
2.添加配置文件:log4net.config
1 23 4 5 7 86 9 10 11 12 1314 15 107 10816 17 18 10019 20 21 22 23 24 25 26 3027 28 29 31 38 3932 33 34 35 3736 40 47 4841 42 43 44 4645 49 56 5750 51 52 53 5554 58 65 6659 60 61 62 6463 67 74 75 83 8468 69 70 71 7372 85 92 9386 87 88 89 9190 94 9995 96 97 98 101 102 106103 104 105
3.在项目中的Properties中AssemblyInfo.cs的末尾添加
1 [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
4.添加实体类:LogEntity.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 6 namespace log4net保存到数据库中 7 { 8 public class LogEntity 9 {10 public string UserID { get; set; }11 public string UserName { get; set; }12 13 public string Message { get; set; }14 }15 }
5.添加扩展类:MyPatternConverter.cs , MyLayout.cs 来扩展PatternLayout
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using log4net.Layout.Pattern; 6 using System.Reflection; 7 8 namespace log4net保存到数据库中 9 {10 public class MyPatternConverter:PatternLayoutConverter11 {12 protected override void Convert(System.IO.TextWriter writer, log4net.Core.LoggingEvent loggingEvent)13 {14 if (Option != null)15 WriteObject(writer, loggingEvent.Repository, LookupProperty(Option, loggingEvent));16 else17 WriteDictionary(writer, loggingEvent.Repository, loggingEvent.GetProperties());18 }19 20 //通过反射获取传入的日志对象的某个属性的值21 private object LookupProperty(string property,log4net.Core.LoggingEvent loggingEvent)22 {23 object propertyvalue = string.Empty;24 PropertyInfo propertyInfo = loggingEvent.MessageObject.GetType().GetProperty(property);25 26 if (propertyInfo != null)27 propertyvalue = propertyInfo.GetValue(loggingEvent.MessageObject, null);28 return propertyvalue;29 }30 }31 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using log4net.Layout; 6 7 namespace log4net保存到数据库中 8 { 9 public class MyLayout:PatternLayout10 {11 public MyLayout()12 {13 this.AddConverter("Property", typeof(MyPatternConverter));14 }15 }16 }
6.添加使用类:LogHelper.cs
1 using System; 2 using System.Diagnostics; 3 using System.IO; 4 5 using log4net; 6 7 namespace log4net保存到数据库中 8 { 9 public class LogHelper 10 { 11 /// 12 /// LoggerName 13 /// 14 public static string LoggerName = string.Empty; 15 /// 16 /// 用户ID 17 /// 18 public static string UserID = string.Empty; 19 /// 20 /// 用户名称 21 /// 22 public static string UserName = string.Empty; 23 24 private static ILog iLog; 25 private static LogEntity logEntity; 26 27 /// 28 /// 接口 29 /// 30 private static ILog log 31 { 32 get 33 { 34 35 if (iLog == null) 36 { 37 iLog = log4net.LogManager.GetLogger(LoggerName); 38 } 39 else 40 { 41 if (iLog.Logger.Name != LoggerName) 42 { 43 iLog = log4net.LogManager.GetLogger(LoggerName); 44 } 45 } 46 47 return iLog; 48 } 49 } 50 51 /// 52 /// 构造消息实体 53 /// 54 /// 55 /// 56 private static LogEntity BuildMessageMode(string message) 57 { 58 if (logEntity == null) 59 { 60 logEntity = new LogEntity(); 61 logEntity.UserID = UserID; 62 logEntity.UserName = UserName; 63 logEntity.Message = message; 64 } 65 else 66 logEntity.Message = message; 67 68 return logEntity; 69 } 70 71 /// 72 /// 调试 73 /// 74 /// 消息 75 public static void Debug(string message) 76 { 77 if (log.IsDebugEnabled) 78 log.Debug(BuildMessageMode(message)); 79 } 80 81 /// 82 /// 调试 83 /// 84 /// 消息 85 /// 异常 86 public static void Debug(string message, Exception ex) 87 { 88 if (log.IsDebugEnabled) 89 log.Debug(BuildMessageMode(message), ex); 90 } 91 92 /// 93 /// 信息 94 /// 95 /// 消息 96 public static void Info(string message) 97 { 98 if (log.IsInfoEnabled) 99 log.Info(BuildMessageMode(message));100 }101 102 /// 103 /// 信息104 /// 105 /// 消息106 /// 异常107 public static void Info(string message, Exception ex)108 {109 if (log.IsInfoEnabled)110 log.Info(BuildMessageMode(message), ex);111 }112 113 /// 114 /// 一般错误115 /// 116 /// 消息117 public static void Error(string message)118 {119 if (log.IsErrorEnabled)120 log.Error(BuildMessageMode(message));121 }122 123 /// 124 /// 一般错误125 /// 126 /// 消息127 /// 异常128 public static void Error(string message, Exception exception)129 {130 if (log.IsErrorEnabled)131 log.Error(BuildMessageMode(message), exception);132 }133 134 /// 135 /// 警告136 /// 137 /// 消息138 public static void Warn(string message)139 {140 if (log.IsWarnEnabled)141 log.Warn(BuildMessageMode(message));142 }143 144 /// 145 /// 警告146 /// 147 /// 消息148 /// 异常149 public static void Warn(string message, Exception ex)150 {151 if (log.IsWarnEnabled)152 log.Warn(BuildMessageMode(message), ex);153 }154 155 /// 156 /// 严重157 /// 158 /// 消息159 public static void Fatal(string message)160 {161 if (log.IsFatalEnabled)162 log.Fatal(BuildMessageMode(message));163 }164 165 /// 166 /// 严重167 /// 168 /// 消息169 /// 异常170 public static void Fatal(string message, Exception ex)171 {172 if (log.IsFatalEnabled)173 log.Fatal(BuildMessageMode(message), ex);174 }175 }176 }
7.在Default.aspx.cs中使用:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using log4net; 8 9 namespace log4net保存到数据库中10 {11 public partial class Default : System.Web.UI.Page12 {13 protected void Page_Load(object sender, EventArgs e)14 {15 LogHelper.LoggerName = typeof(Default).ToString();16 LogHelper.UserID = "12345";17 LogHelper.UserName = "me";18 int i = 0;19 try20 {21 22 int b = 3 / i;23 }24 catch (Exception ex)25 {26 LogHelper.Error(ex.Message, ex);27 }28 LogHelper.Fatal("Fatal",new Exception("异常信息"));29 LogHelper.Info("Info");30 LogHelper.Warn("Warn");31 LogHelper.Debug("Debug");32 }33 }34 }
8.配置数据库SQL Server2012:创建数据库log4netDB
1 CREATE TABLE [dbo].[Log] ( 2 [Id] [int] IDENTITY (1, 1) NOT NULL, 3 [Date] [datetime] NOT NULL, 4 [Thread] [varchar] (255) NOT NULL, 5 [Level] [varchar] (50) NOT NULL, 6 [Logger] [varchar] (255) NOT NULL, 7 [UserID] [varchar] (20) NOT NULL, 8 [UserName] [varchar] (50) NOT NULL, 9 [Message] [varchar] (4000) NOT NULL,10 [Exception] [varchar] (2000) NULL11 )
9.运行
