在請求WebApi 的時候,我們更想知道在請求數據的時候,調用了哪個介面傳了什麼參數過來,調用這個Action花了多少時間,有沒有人惡意請求。我們可以通過記錄日誌,對Action進行優化,可以通過日誌追蹤是哪個用戶或ip惡意請求。 在項目中引用log4net.dll 定義一個WebApiMonito ...
在請求WebApi 的時候,我們更想知道在請求數據的時候,調用了哪個介面傳了什麼參數過來,調用這個Action花了多少時間,有沒有人惡意請求。我們可以通過記錄日誌,對Action進行優化,可以通過日誌追蹤是哪個用戶或ip惡意請求。
在項目中引用log4net.dll
定義一個WebApiMonitorLog ,監控日誌對象
/// <summary> /// 監控日誌對象 /// </summary> public class WebApiMonitorLog { public string ControllerName { get; set; } public string ActionName { get; set; } public DateTime ExecuteStartTime { get; set; } public DateTime ExecuteEndTime { get; set; } /// <summary> /// 請求的Action 參數 /// </summary> public Dictionary<string, object> ActionParams { get; set; } /// <summary> /// Http請求頭 /// </summary> public string HttpRequestHeaders { get; set; } /// <summary> /// 請求方式 /// </summary> public string HttpMethod { get; set; } /// <summary> /// 請求的IP地址 /// </summary> public string IP { get; set; } /// <summary> /// 獲取監控指標日誌 /// </summary> /// <param name="mtype"></param> /// <returns></returns> public string GetLoginfo() { string Msg = @" Action執行時間監控: ControllerName:{0}Controller ActionName:{1} 開始時間:{2} 結束時間:{3} 總 時 間:{4}秒 Action參數:{5} Http請求頭:{6} 客戶端IP:{7}, HttpMethod:{8} "; return string.Format(Msg, ControllerName, ActionName, ExecuteStartTime, ExecuteEndTime, (ExecuteEndTime - ExecuteStartTime).TotalSeconds, GetCollections(ActionParams), HttpRequestHeaders, IP, HttpMethod); } /// <summary> /// 獲取Action 參數 /// </summary> /// <param name="Collections"></param> /// <returns></returns> public string GetCollections(Dictionary<string, object> Collections) { string Parameters = string.Empty; if (Collections == null || Collections.Count == 0) { return Parameters; } foreach (string key in Collections.Keys) { Parameters += string.Format("{0}={1}&", key, Collections[key]); } if (!string.IsNullOrWhiteSpace(Parameters) && Parameters.EndsWith("&")) { Parameters = Parameters.Substring(0, Parameters.Length - 1); } return Parameters; } /// <summary> /// 獲取IP /// </summary> /// <returns></returns> public string GetIP() { string ip = string.Empty; if (!string.IsNullOrEmpty(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"])) ip = Convert.ToString(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]); if (string.IsNullOrEmpty(ip)) ip = Convert.ToString(System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]); return ip; } }WebApiMonitorLog
定義一個LoggerHelper,日誌幫助類
public class LoggerHelper { private static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo"); private static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror"); private static readonly log4net.ILog logmonitor = log4net.LogManager.GetLogger("logmonitor"); public static void Error(string ErrorMsg, Exception ex = null) { if (ex != null) { logerror.Error(ErrorMsg, ex); } else { logerror.Error(ErrorMsg); } } public static void Info(string Msg) { loginfo.Info(Msg); } public static void Monitor(string Msg) { logmonitor.Info(Msg); } }LoggerHelper
定義一個WebApiTrackerAttribute類,繼承於ActionFilterAttribute
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)] public class WebApiTrackerAttribute : ActionFilterAttribute { private readonly string Key = "_thisWebApiOnActionMonitorLog_"; public override void OnActionExecuting(HttpActionContext actionContext) { base.OnActionExecuting(actionContext); WebApiMonitorLog MonLog = new WebApiMonitorLog(); MonLog.ExecuteStartTime = DateTime.Now; //獲取Action 參數 MonLog.ActionParams = actionContext.ActionArguments; MonLog.HttpRequestHeaders = actionContext.Request.Headers.ToString(); MonLog.HttpMethod = actionContext.Request.Method.Method; actionContext.Request.Properties[Key] = MonLog; var form = System.Web.HttpContext.Current.Request.Form; #region 如果參數是實體對象,獲取序列化後的數據 Stream stream = actionContext.Request.Content.ReadAsStreamAsync().Result; Encoding encoding = Encoding.UTF8; stream.Position = 0; string responseData = ""; using (StreamReader reader = new StreamReader(stream, encoding)) { responseData = reader.ReadToEnd().ToString(); } if (!string.IsNullOrWhiteSpace(responseData) && !MonLog.ActionParams.ContainsKey("__EntityParamsList__")) { MonLog.ActionParams["__EntityParamsList__"] = responseData; } #endregion } public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) { WebApiMonitorLog MonLog = actionExecutedContext.Request.Properties[Key] as WebApiMonitorLog; MonLog.ExecuteEndTime = DateTime.Now; MonLog.ActionName = actionExecutedContext.ActionContext.ActionDescriptor.ActionName; MonLog.ControllerName = actionExecutedContext.ActionContext.ActionDescriptor.ControllerDescriptor.ControllerName; LoggerHelper.Monitor(MonLog.GetLoginfo()); if (actionExecutedContext.Exception != null) { string Msg = string.Format(@" 請求【{0}Controller】的【{1}】產生異常: Action參數:{2} Http請求頭:{3} 客戶端IP:{4}, HttpMethod:{5} ", MonLog.ControllerName, MonLog.ActionName, MonLog.GetCollections(MonLog.ActionParams), MonLog.HttpRequestHeaders, MonLog.GetIP(), MonLog.HttpMethod); LoggerHelper.Error(Msg, actionExecutedContext.Exception); } } }WebApiTrackerAttribute
新建一個log4net.config
<?xml version="1.0"?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <log4net> <!--錯誤日誌--> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="log\\LogError\\"/> <appendToFile value="true"/> <rollingStyle value="Date"/> <datePattern value="yyyy\\yyyyMM\\yyyyMMdd'.txt'"/> <staticLogFileName value="false"/> <param name="MaxSizeRollBackups" value="100"/> <layout type="log4net.Layout.PatternLayout"> <!--每條日誌末尾的文字說明--> <!--輸出格式--> <!--樣例:2008-03-26 13:42:32,111 [10] INFO Log4NetDemo.MainClass [(null)] - info--> <conversionPattern value="%newline %n記錄時間:%date %n線程ID:[%thread] %n日誌級別: %-5level %n錯誤描述:%message%newline %n"/> </layout> </appender> <!--Info日誌--> <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="Log\\LogInfo\\" /> <param name="AppendToFile" value="true" /> <param name="MaxFileSize" value="10240" /> <param name="MaxSizeRollBackups" value="100" /> <param name="StaticLogFileName" value="false" /> <param name="DatePattern" value="yyyy\\yyyyMM\\yyyyMMdd'.txt'" /> <param name="RollingStyle" value="Date" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%newline %n記錄時間:%date %n線程ID:[%thread] %n日誌級別: %-5level %n日誌描述:%message%newline %n"/> </layout> </appender> <!--監控日誌--> <appender name="MonitorAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="Log\\LogMonitor\\" /> <param name="AppendToFile" value="true" /> <param name="MaxFileSize" value="10240" /> <param name="MaxSizeRollBackups" value="100" /> <param name="StaticLogFileName" value="false" /> <param name="DatePattern" value="yyyy\\yyyyMM\\yyyyMMdd'.txt'" /> <param name="RollingStyle" value="Date" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%newline %n記錄時間:%date %n線程ID:[%thread] %n日誌級別: %-5level %n跟蹤描述:%message%newline %n"/> </layout> </appender> <!--Error日誌--> <logger name="logerror"> <level value="ERROR" /> <appender-ref ref="RollingLogFileAppender" /> </logger> <!--Info日誌--> <logger name="loginfo"> <level value="INFO" /> <appender-ref ref="InfoAppender" /> </logger> <!--監控日誌--> <logger name="logmonitor"> <level value="Monitor" /> <appender-ref ref="MonitorAppender" /> </logger> </log4net> </configuration>log4net.config
然後引用監控,在Global.asax 裡加上這段
GlobalConfiguration.Configuration.Filters.Add(new WebApiTrackerAttribute()); AreaRegistration.RegisterAllAreas();
最後在需要監控的控制器上加上 WebApiTracker
每次在調用這個監控下的Action 時,都會有日誌記錄,像這樣滴
在項目下有個log 文件夾
日誌記錄
參考於:http://www.cnblogs.com/lc-chenlong/p/4228639.html log4net 記錄MVC監控日誌