本文Log4Net介紹了基礎的方式,大數據量生產環境不能使用,中等日誌量請日誌單庫。 但願愛技術的你不要錯過exceptionless和ELKhtml
第四節開始簡單配置大牛們推薦的了ExceptionLess, 一款開源分佈式日誌系統。git
日誌系統對於任何項目都是必不可少的,不管對於測試階段的debug,性能測試,執行時間,操做記錄仍是線上的問題排查,訪問記錄等,日誌系統都扮演着重要的角色。本篇分享的目的是能幫助須要的人快速搭建本身的LogSystem.,僅供參考。 先上個圖唄,自認爲頁面還算清爽吧:github
個人LogSystem使用Log4net入庫的方式,網上特別多的分享,可是能完整運行下來的真是不多,因此如今須要和之後用得上的小夥伴抓緊收藏咯。web
Log4Net存日誌的方式,給人的感受實在是不實用,IT行業不都求一個自動化嗎?廢話不說了,先上Log4net入庫系統的代碼。sql
LogSystem數據庫結構,個人建議是一個項目一個表。數據庫
在Log組件中,你須要這樣幾個類。下面分別給出代碼:app
LogContent.cs,這裏定義了Log實體,在實體化實體的時候,經過給構造函數傳參建立好這個對象。註釋很詳細了框架
1 using System; 2 3 namespace LogComponent 4 { 5 public class LogContent 6 { 7 8 public LogContent(string logLevel, string logMsg, string logModule, string description, string userName) 9 { 10 LogLevel = logLevel; 11 UserName = userName; 12 Description = description; 13 LogMsg = logMsg; 14 LogModule = logModule; 15 } 16 17 /// <summary> 18 /// 日誌級別 19 /// </summary> 20 public string LogLevel { get; set; } 21 22 /// <summary> 23 /// 日誌消息 24 /// </summary> 25 public string LogMsg { get; set; } 26 27 /// <summary> 28 /// 系統登錄用戶 29 /// </summary> 30 public string UserName { get; set; } 31 32 /// <summary> 33 /// 日誌描述信息 34 /// </summary> 35 public string Description { get; set; } 36 37 /// <summary> 38 /// 記錄時間 39 /// </summary> 40 public DateTime LogDate { get; set; } 41 42 /// <summary> 43 /// 模塊名稱 44 /// </summary> 45 public string LogModule { get; set; } 46 } 47 }
LogHelper.cs,定義了日誌級別,和寫入方法less
1 [assembly: log4net.Config.XmlConfigurator(Watch = true,ConfigFile = "log4net.config")] 2 namespace LogComponent 3 { 4 public class LogHelper 5 { 6 static log4net.ILog log = log4net.LogManager.GetLogger("myLogger"); 7 8 /// <summary> 9 /// 異常日誌 10 /// </summary> 11 /// <param name="logMsg">日誌信息</param> 12 /// <param name="logModule">代碼模塊</param> 13 /// <param name="description">其餘描述</param> 14 /// <param name="userName">用戶名</param> 15 public static void LogError(string logMsg, string logModule, string description = "", string userName = "") 16 { 17 log.Error(new LogContent("Error", SubLogString(logMsg), logModule, SubLogString(description), userName)); 18 } 19 20 public static void LogInfo(string logMsg, string logModule, string description = "", string userName = "") 21 { 22 log.Info(new LogContent("Info", SubLogString(logMsg), logModule, SubLogString(description), userName)); 23 } 24 25 public static void LogWarn(string logMsg, string logModule, string description = "", string userName = "") 26 { 27 log.Warn(new LogContent("Warn", SubLogString(logMsg), logModule, SubLogString(description), userName)); 28 } 29 30 public static void LogDebug(string logMsg, string logModule, string description = "", string userName = "") 31 { 32 log.Debug(new LogContent("Debug", SubLogString(logMsg), logModule, SubLogString(description), userName)); 33 } 34 35 private static string SubLogString(string str) 36 { 37 if (str.Length > 1500) 38 { 39 return str.Substring(0, 1500); 40 } 41 return str; 42 } 43 } 44 }
MessagePartternConverter.cs分佈式
1 using log4net.Core; 2 using log4net.Layout.Pattern; 3 using System.IO; 4 using System.Reflection; 5 namespace LogComponent 6 { 7 class MessagePatternConverter : PatternLayoutConverter 8 { 9 protected override void Convert(TextWriter writer, LoggingEvent loggingEvent) 10 { 11 if (Option != null) 12 { 13 // Write the value for the specified key 14 WriteObject(writer, loggingEvent.Repository, LookupProperty(Option, loggingEvent)); 15 } 16 else 17 { 18 // Write all the key value pairs 19 WriteDictionary(writer, loggingEvent.Repository, loggingEvent.GetProperties()); 20 } 21 } 22 /// <summary> 23 /// 經過反射獲取傳入的日誌對象的某個屬性的值 24 /// </summary> 25 /// <param name="property"></param> 26 /// <returns></returns> 27 private object LookupProperty(string property, log4net.Core.LoggingEvent loggingEvent) 28 { 29 object propertyValue = string.Empty; 30 PropertyInfo propertyInfo = loggingEvent.MessageObject.GetType().GetProperty(property); 31 if (propertyInfo != null) 32 propertyValue = propertyInfo.GetValue(loggingEvent.MessageObject, null); 33 return propertyValue; 34 } 35 } 36 }
MyLayout.cs
1 using log4net.Layout; 2 namespace LogComponent 3 { 4 class MyLayout : PatternLayout 5 { 6 public MyLayout() 7 { 8 this.AddConverter("property", typeof(MessagePatternConverter)); 9 } 10 } 11 }
其實看到這裏,最重要的並非代碼了,核心部分Log4net都幫咱們寫好了,關鍵在於你的配置,下面是log4net.config的內容。拿到你的web項目裏是同樣用的。可是不要忘了在你的項目中引用nuget:log4net喲。
log4net.config以下:在其中主要配置了log入庫的參數和sql語句,固然還有sql鏈接。註釋已經很詳細了
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <configSections> 4 <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> 5 </configSections> 6 <log4net> 7 <root > 8 <level value="Debug"/> 9 <appender-ref ref="ADONetAppender"/> 10 </root> 11 <logger name="myLogger"> 12 <level value="Debug"/> 13 <appender-ref ref="ADONetAppender"/> 14 </logger> 15 <appender name="ADONetAppender" type="log4net.Appender.ADONetAppender,log4net"> 16 <!--BufferSize爲緩衝區大小,只有日誌記錄超value條纔會一塊寫入到數據庫--> 17 <bufferSize value="1"/> 18 <!--或寫爲<param name="BufferSize" value="1" />--> 19 <!--引用--> 20 <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> 21 <!--鏈接數據庫字符串--> 22 <connectionString value="Data Source=115.29.54.31;Initial Catalog=LogSystem;uid=sa;pwd=sa.;MultipleActiveResultSets=True"/> 23 <!--插入到表Log--> 24 <commandText value="INSERT INTO HdPubLog ([LogDate],[LogMsg],[UserName],[Description],[LogLevel],[LogModule]) VALUES (@log_date,@LogMsg,@UserName,@Description,@LogLevel,@LogModule)"/> 25 <parameter> 26 <parameterName value="@log_date"/> 27 <dbType value="DateTime"/> 28 <layout type="log4net.Layout.RawTimeStampLayout"/> 29 <!--獲取log4net中提供的日誌時間RawTimeStampLayout爲默認的時間輸出格式--> 30 </parameter> 31 <parameter> 32 <parameterName value="@LogMsg"/> 33 <dbType value="String"/> 34 <size value="1510"/> 35 <layout type="LogComponent.MyLayout, LogComponent"> 36 <param name="ConversionPattern" value="%property{LogMsg}"/> 37 </layout> 38 </parameter> 39 <parameter> 40 <parameterName value="@UserName"/> 41 <dbType value="String"/> 42 <size value="50"/> 43 <layout type="LogComponent.MyLayout, LogComponent"> 44 <param name="ConversionPattern" value="%property{UserName}"/> 45 </layout> 46 </parameter> 47 <parameter> 48 <parameterName