1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | webconfig 里的配置! < log4net > < logger name = "myLogger" > < level value = "All" /> < appender-ref ref = "AdoNetAppender_SqlServer" /> </ logger > < appender name = "AdoNetAppender_SqlServer" type = "log4net.Appender.AdoNetAppender" > <!-- BufferSize 涓虹紦鍐插尯澶у皬锛屽彧鏈夋棩蹇楄褰曡秴5 鏉℃墠浼氫竴鍧楀啓鍏ュ埌鏁版嵁搴?--> <!-- 鎴栧啓涓?param name="BufferSize" value="10" /> --> < bufferSize value = "50" /> <!-- 寮曠敤 --> <!--2.0杩欐槸瀵瑰簲sql2008 濡傛槸2000鎴?005鍙﹀閰嶇疆--> < connectionType value = "System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral,PublicKeyToken=b77a5c561934e089" /> <!-- 杩炴帴鏁版嵁搴撳瓧绗︿覆 --> < connectionString value = "Data Source=.\SQLEXPRESS;Initial Catalog=CaseManage;User ID=sa;Password=1234" /> <!-- 鎻掑叆鍒拌〃Log --> < commandText value = "INSERT INTO X_Log ([TIMESTAMP],[EVENTCATEGORY],[COMPUTERNAME],[MAC_ADDRESS],[USERNAME],[SOURCE],[DESCRIPTION],[COLLECTDATE]) VALUES (@log_date, @EventCategory, @ComputerName,@Mac_Address,@UserName,@Source,@Description,@CollectDate) " /> <!-- 鏃ュ織绫诲瀷锛岃繖閲屽潎涓? --> <!-- 鏃ュ織璁板綍鏃堕棿锛孯awTimeStampLayout 涓洪粯璁ょ殑鏃堕棿杈撳嚭鏍煎紡 --> < parameter > < parameterName value = "@log_date" /> < dbType value = "DateTime" /> < layout type = "log4net.Layout.RawTimeStampLayout" /> <!-- /杩欓噷鍛㈡槸鑾峰彇log4net 涓彁渚涚殑鏃ュ織鏃堕棿 --> </ parameter > <!-- 鏃ュ織鍒嗙被鎻忚堪 --> < parameter > < parameterName value = "@EventCategory" /> < dbType value = "String" /> < size value = "50" /> < layout type = "AppBox.MyLayout,AppBox" > < conversionPattern value = "%property{EventCategory}" /> </ layout > </ parameter > Source < parameter >< parameterName value = "@Source" />< dbType value = "String" />< size value = "5000" />< layout type = "AppBox.MyLayout,AppBox" >< conversionPattern value = "%property{Source}" /></ layout ></ parameter > <!-- 璁$畻鏈篒P --> < parameter >< parameterName value = "@ComputerName" />< dbType value = "String" />< size value = "50" />< layout type = "AppBox.MyLayout,AppBox" >< conversionPattern value = "%property{ComputerName}" /></ layout ></ parameter > <!-- 璁$畻鏈篗ac 淇℃伅 --> < parameter >< parameterName value = "@Mac_Address" />< dbType value = "String" />< size value = "50 " />< layout type = "AppBox.MyLayout, AppBox" >< conversionPattern value = "%property{Mac_Address}" /></ layout ></ parameter > <!-- 鐧婚檰绯荤粺鐢ㄦ埛鍚?--> < parameter >< parameterName value = "@UserName" />< dbType value = "String" />< size value = "50" />< layout type = "AppBox.MyLayout,AppBox" >< conversionPattern value = "%property{UserName}" /></ layout ></ parameter > <!-- 浜嬩欢鎻忚堪 --> < parameter >< parameterName value = "@Description " />< dbType value = "String" />< size value = "4000" />< layout type = "AppBox.MyLayout, AppBox" >< conversionPattern value = "%property{Description}" /></ layout ></ parameter >< parameter >< parameterName value = "@CollectDate" />< dbType value = "DateTime" />< layout type = "log4net.Layout.RawTimeStampLayout" /></ parameter ></ appender > </ log4net > |
loghelper.cs文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | public class LogHelper { public LogHelper() { SetConfig(); } private static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger( "myLogger" ); private static bool IsLoadConfig = false ; private static void SetConfig() { log4net.Config.DOMConfigurator.Configure(); } /// <summary> /// 记录日志 /// </summary> /// <param name="info">提示信息</param> public static void WriteLog( string info) { if (!IsLoadConfig) { SetConfig(); IsLoadConfig = true ; } if (loginfo.IsInfoEnabled) { loginfo.Info(info); } } /// <summary> /// 记录日志 /// </summary> /// <param name="info">提示信息</param> public static void WriteLog( object info) { if (!IsLoadConfig) { SetConfig(); IsLoadConfig = true ; } if (loginfo.IsInfoEnabled) { loginfo.Info(info); } } /// <summary> /// 写日志 /// </summary> /// <param name="LogType">日志类型</param> /// <param name="Message"></param> public void WriteLog( string LogType, string Message, string IP, string UserName, string RequestUrl ) { LogContent logmodel = new LogContent(); PageBase Base= new PageBase(); logmodel.EventCategory = LogType; logmodel.ComputerName = IP; logmodel.Source = RequestUrl; logmodel.Mac_Address = Base.GetMacAddress(); logmodel.UserName = UserName; logmodel.Description = Message; LogHelper.WriteLog(logmodel); } /// <summary> /// 记录异常 /// </summary> /// <param name="info">错误</param> /// <param name="ex">Exception</param> public static void WriteLog( string info, Exception ex) { if (!IsLoadConfig) { SetConfig(); IsLoadConfig = true ; } if (loginfo.IsErrorEnabled) { loginfo.Error(info, ex); } } } |
logcontent.cs文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | /// <summary> /// 包含了所有的自定字段属性 /// </summary> public partial class LogContent { /// <summary> /// 日志分类描述,自定义 /// </summary> public string EventCategory { get ; set ; } /// <summary> /// 日志分类描述,自定义 /// </summary> public string Source { get ; set ; } /// <summary> /// 计算机IP /// </summary> public string ComputerName { get ; set ; } /// <summary> /// 计算机Mac 地址 /// </summary> public string Mac_Address { get ; set ; } /// <summary> /// 系统登陆用户 /// </summary> public string UserName { get ; set ; } /// <summary> /// 日志描述信息 /// </summary> public string Description { get ; set ; } } |
摘自某系统!