123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using System.Collections.Specialized;
- using System.Globalization;
- namespace EMIS.Web.Filters
- {
- [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
- public class StatisticsTrackerAttribute : ActionFilterAttribute, IExceptionFilter
- {
- private readonly string Key = "_thisOnActionMonitorLog_";
- private readonly NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger();
- public override void OnActionExecuting(ActionExecutingContext filterContext)
- {
- MonitorLog MonLog = new MonitorLog();
- MonLog.ExecuteStartTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.ffff", DateTimeFormatInfo.InvariantInfo));
- MonLog.ControllerName = filterContext.RouteData.Values["controller"] as string;
- MonLog.ActionName = filterContext.RouteData.Values["action"] as string;
- filterContext.Controller.ViewData[Key] = MonLog;
- }
- public override void OnActionExecuted(ActionExecutedContext filterContext)
- {
- MonitorLog MonLog = filterContext.Controller.ViewData[Key] as MonitorLog;
- MonLog.ExecuteEndTime = DateTime.Now;
- MonLog.FormCollections = filterContext.HttpContext.Request.Form;//form表单提交的数据
- MonLog.QueryCollections = filterContext.HttpContext.Request.QueryString;//Url 参数
- _logger.Info(MonLog.GetLoginfo());
- }
- public override void OnResultExecuting(ResultExecutingContext filterContext)
- {
- MonitorLog MonLog = filterContext.Controller.ViewData[Key] as MonitorLog;
- MonLog.ExecuteStartTime = DateTime.Now;
- }
- public override void OnResultExecuted(ResultExecutedContext filterContext)
- {
- MonitorLog MonLog = filterContext.Controller.ViewData[Key] as MonitorLog;
- MonLog.ExecuteEndTime = DateTime.Now;
- _logger.Info(MonLog.GetLoginfo(MonitorLog.MonitorType.View));
- filterContext.Controller.ViewData.Remove(Key);
- }
- public void OnException(ExceptionContext filterContext)
- {
- if (!filterContext.ExceptionHandled)
- {
- string ControllerName = string.Format("{0}Controller", filterContext.RouteData.Values["controller"] as string);
- string ActionName = filterContext.RouteData.Values["action"] as string;
- string ErrorMsg = string.Format("在执行 controller[{0}] 的 action[{1}] 时产生异常", ControllerName, ActionName);
- _logger.ErrorException(ErrorMsg, filterContext.Exception);
- }
- }
- }
- /// <summary>
- /// 监控日志对象
- /// </summary>
- public class MonitorLog
- {
- public string ControllerName
- {
- get;
- set;
- }
- public string ActionName
- {
- get;
- set;
- }
- public DateTime ExecuteStartTime
- {
- get;
- set;
- }
- public DateTime ExecuteEndTime
- {
- get;
- set;
- }
- /// <summary>
- /// Form 表单数据
- /// </summary>
- public NameValueCollection FormCollections
- {
- get;
- set;
- }
- /// <summary>
- /// URL 参数
- /// </summary>
- public NameValueCollection QueryCollections
- {
- get;
- set;
- }
- /// <summary>
- /// 监控类型
- /// </summary>
- public enum MonitorType
- {
- Action = 1,
- View = 2
- }
- /// <summary>
- /// 获取监控指标日志
- /// </summary>
- /// <param name="mtype"></param>
- /// <returns></returns>
- public string GetLoginfo(MonitorType mtype = MonitorType.Action)
- {
- string ActionView = "Action执行时间监控:";
- string Name = "Action";
- if (mtype == MonitorType.View)
- {
- ActionView = "View视图生成时间监控:";
- Name = "View";
- }
- string Msg = @"
- {0}
- ControllerName:{1}Controller
- {8}Name:{2}
- 开始时间:{3}
- 结束时间:{4}
- 总 时 间:{5}秒
- Form表单数据:{6}
- URL参数:{7}
- ";
- return string.Format(Msg,
- ActionView,
- ControllerName,
- ActionName,
- ExecuteStartTime,
- ExecuteEndTime,
- (ExecuteEndTime - ExecuteStartTime).TotalSeconds,
- GetCollections(FormCollections),
- GetCollections(QueryCollections),
- Name);
- }
- /// <summary>
- /// 获取Post 或Get 参数
- /// </summary>
- /// <param name="Collections"></param>
- /// <returns></returns>
- public string GetCollections(NameValueCollection 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;
- }
- }
- }
|