Log4Net是常用的功能強大的日誌外掛,該外掛提供了幾個預設欄位
大家可能都用過Log4Net外掛來記錄日誌,該外掛預設提供了這幾個欄位@log_date, @thread, @log_level, @logger, @message, @exception, 但有時這幾個欄位不能滿足我們記錄日誌的需求,需要擴充套件我們所需的其他欄位,並記錄到資料庫。比如,我需要記錄UserID,UnitCode,MenuID,OperateType等。下面按步驟介紹一下如何在Log4Net元件下,自定義欄位並儲存到資料庫:
第一步:在資料庫日誌表中新增自定義欄位
第二步:建立Custom實體ActionLoggerInfo
public class ActionLoggerInfo { public int UserID { get; set; } public string UnitCode { get; set; } public int MenuID { get; set; } public int OperaterType { get; set; } public string sMessage { get; set; } public ActionLoggerInfo(int userId, string unitCode, int menuId, int operaterType,string smessage) { this.UserID = userId; this.UnitCode = unitCode; this.MenuID = menuId; this.OperaterType = operaterType; this.sMessage = smessage; } }
第三步:建立Custom轉換器ActionConverter
/*名稱空間引用*/ using log4net; using log4net.Layout; using log4net.Layout.Pattern; using log4net.Core; /*程式碼*/ public class ActionConverter : PatternLayoutConverter { protected override void Convert(System.IO.TextWriter writer, LoggingEvent loggingEvent) { var actionInfo = loggingEvent.MessageObject as ActionLoggerInfo; if (actionInfo == null) { writer.Write(""); } else { switch (this.Option.ToLower()) { case "userid": writer.Write(actionInfo.UserID); break; case "unitcode": writer.Write(actionInfo.UnitCode); break; case "menuid": writer.Write(actionInfo.MenuID); break; case "operatertype": writer.Write(actionInfo.OperaterType); break; case "smessage": writer.Write(actionInfo.sMessage); break; default: writer.Write(""); break; } } } }
第四步:建立ActionLayoutPattern
/*名稱空間引用*/ using log4net; using log4net.Layout; using log4net.Layout.Pattern; using log4net.Core; /*程式碼*/ public class ActionLayoutPattern : PatternLayout { public ActionLayoutPattern() { this.AddConverter("actionInfo", typeof(ActionConverter)); } }
第五步:配置log4net.config
<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender"> <bufferSize value="1" /> <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <connectionString value="DATABASE=AT_GGJPlat3;SERVER=192.168.**.***;UID=**;PWD=***;Connect Timeout=15;" /> <commandText value="INSERT INTO *****Log([dtDate],[sThread],[sLevel],[sLogger],[sMessage],[sException],[UserID],[UnitCode],[MenuID],[OperaterType])
VALUES (@log_date, @thread, @log_level, @logger, @message, @exception,@userid,@unitcode,@menuid,@operatertype)" /> <parameter> <parameterName value="@log_date" /> <dbType value="DateTime" /> <layout type="log4net.Layout.RawTimeStampLayout" /> </parameter> <parameter> <parameterName value="@thread" /> <dbType value="String" /> <size value="100" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%t" /> </layout> </parameter> <parameter> <parameterName value="@log_level" /> <dbType value="String" /> <size value="200" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%p" /> </layout> </parameter> <parameter> <parameterName value="@logger" /> <dbType value="String" /> <size value="500" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%logger" /> </layout> </parameter> <parameter> <parameterName value="@message" /> <dbType value="String" /> <size value="3000" /> <layout type="ProvincePlat.CommonClass.Logger.ActionLayoutPattern"> <conversionPattern value="%actionInfo{smessage}"/> </layout> </parameter> <parameter> <parameterName value="@exception" /> <dbType value="String" /> <size value="4000" /> <layout type="log4net.Layout.ExceptionLayout" /> </parameter> <parameter> <parameterName value="@userid" /> <dbType value="Int32"/> <layout type="ProvincePlat.CommonClass.Logger.ActionLayoutPattern"> <conversionPattern value="%actionInfo{userid}"/> </layout> </parameter> <parameter> <parameterName value="@unitcode" /> <dbType value="String" /> <size value="10" /> <layout type="ProvincePlat.CommonClass.Logger.ActionLayoutPattern"> <conversionPattern value="%actionInfo{unitcode}"/> </layout> </parameter> <parameter> <parameterName value="@menuid" /> <dbType value="Int32"/> <layout type="ProvincePlat.CommonClass.Logger.ActionLayoutPattern"> <conversionPattern value="%actionInfo{menuid}"/> </layout> </parameter> <parameter> <parameterName value="@operatertype" /> <dbType value="Int32"/> <layout type="ProvincePlat.CommonClass.Logger.ActionLayoutPattern"> <conversionPattern value="%actionInfo{operatertype}"/> </layout> </parameter> </appender>
第六步:建立Log日誌幫助類,LogUtil
public class LogUtil { public LogUtil() { } private static ActionLoggerInfo _message = null; private static log4net.ILog _log; public static log4net.ILog Log { get { if (_log == null) { _log = LogManager.GetLogger("OperateLogger"); } return _log; } } public static void Debug() { if (Log.IsDebugEnabled) { Log.Debug(_message); } } public static void Error() { if (Log.IsErrorEnabled) { Log.Error(_message); } } public static void Fatal() { if (Log.IsFatalEnabled) { Log.Fatal(_message); } } public static void Info() { if (Log.IsInfoEnabled) { Log.Info(_message); } } public static void Warn() { if (Log.IsWarnEnabled) { Log.Warn(_message); } } public static void SaveMessage(int userId, string unitCode, int menuId, int operaterType,string smessage,int level) { _message = new ActionLoggerInfo(userId, unitCode, menuId, operaterType, smessage); switch (level) { case 1: Info(); break; case 2: Warn(); break; case 3: Error(); break; case 4: Fatal(); break; default: break; } } }
第七步:在具體頁面上呼叫SaveMessage方法,寫入日誌