StatisticsTrackerAttribute.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using System.Collections.Specialized;
  7. using System.Globalization;
  8. namespace EMIS.Web.Filters
  9. {
  10. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
  11. public class StatisticsTrackerAttribute : ActionFilterAttribute, IExceptionFilter
  12. {
  13. private readonly string Key = "_thisOnActionMonitorLog_";
  14. private readonly NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger();
  15. public override void OnActionExecuting(ActionExecutingContext filterContext)
  16. {
  17. MonitorLog MonLog = new MonitorLog();
  18. MonLog.ExecuteStartTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.ffff", DateTimeFormatInfo.InvariantInfo));
  19. MonLog.ControllerName = filterContext.RouteData.Values["controller"] as string;
  20. MonLog.ActionName = filterContext.RouteData.Values["action"] as string;
  21. filterContext.Controller.ViewData[Key] = MonLog;
  22. }
  23. public override void OnActionExecuted(ActionExecutedContext filterContext)
  24. {
  25. MonitorLog MonLog = filterContext.Controller.ViewData[Key] as MonitorLog;
  26. MonLog.ExecuteEndTime = DateTime.Now;
  27. MonLog.FormCollections = filterContext.HttpContext.Request.Form;//form表单提交的数据
  28. MonLog.QueryCollections = filterContext.HttpContext.Request.QueryString;//Url 参数
  29. _logger.Info(MonLog.GetLoginfo());
  30. }
  31. public override void OnResultExecuting(ResultExecutingContext filterContext)
  32. {
  33. MonitorLog MonLog = filterContext.Controller.ViewData[Key] as MonitorLog;
  34. MonLog.ExecuteStartTime = DateTime.Now;
  35. }
  36. public override void OnResultExecuted(ResultExecutedContext filterContext)
  37. {
  38. MonitorLog MonLog = filterContext.Controller.ViewData[Key] as MonitorLog;
  39. MonLog.ExecuteEndTime = DateTime.Now;
  40. _logger.Info(MonLog.GetLoginfo(MonitorLog.MonitorType.View));
  41. filterContext.Controller.ViewData.Remove(Key);
  42. }
  43. public void OnException(ExceptionContext filterContext)
  44. {
  45. if (!filterContext.ExceptionHandled)
  46. {
  47. string ControllerName = string.Format("{0}Controller", filterContext.RouteData.Values["controller"] as string);
  48. string ActionName = filterContext.RouteData.Values["action"] as string;
  49. string ErrorMsg = string.Format("在执行 controller[{0}] 的 action[{1}] 时产生异常", ControllerName, ActionName);
  50. _logger.ErrorException(ErrorMsg, filterContext.Exception);
  51. }
  52. }
  53. }
  54. /// <summary>
  55. /// 监控日志对象
  56. /// </summary>
  57. public class MonitorLog
  58. {
  59. public string ControllerName
  60. {
  61. get;
  62. set;
  63. }
  64. public string ActionName
  65. {
  66. get;
  67. set;
  68. }
  69. public DateTime ExecuteStartTime
  70. {
  71. get;
  72. set;
  73. }
  74. public DateTime ExecuteEndTime
  75. {
  76. get;
  77. set;
  78. }
  79. /// <summary>
  80. /// Form 表单数据
  81. /// </summary>
  82. public NameValueCollection FormCollections
  83. {
  84. get;
  85. set;
  86. }
  87. /// <summary>
  88. /// URL 参数
  89. /// </summary>
  90. public NameValueCollection QueryCollections
  91. {
  92. get;
  93. set;
  94. }
  95. /// <summary>
  96. /// 监控类型
  97. /// </summary>
  98. public enum MonitorType
  99. {
  100. Action = 1,
  101. View = 2
  102. }
  103. /// <summary>
  104. /// 获取监控指标日志
  105. /// </summary>
  106. /// <param name="mtype"></param>
  107. /// <returns></returns>
  108. public string GetLoginfo(MonitorType mtype = MonitorType.Action)
  109. {
  110. string ActionView = "Action执行时间监控:";
  111. string Name = "Action";
  112. if (mtype == MonitorType.View)
  113. {
  114. ActionView = "View视图生成时间监控:";
  115. Name = "View";
  116. }
  117. string Msg = @"
  118. {0}
  119. ControllerName:{1}Controller
  120. {8}Name:{2}
  121. 开始时间:{3}
  122. 结束时间:{4}
  123. 总 时 间:{5}秒
  124. Form表单数据:{6}
  125. URL参数:{7}
  126. ";
  127. return string.Format(Msg,
  128. ActionView,
  129. ControllerName,
  130. ActionName,
  131. ExecuteStartTime,
  132. ExecuteEndTime,
  133. (ExecuteEndTime - ExecuteStartTime).TotalSeconds,
  134. GetCollections(FormCollections),
  135. GetCollections(QueryCollections),
  136. Name);
  137. }
  138. /// <summary>
  139. /// 获取Post 或Get 参数
  140. /// </summary>
  141. /// <param name="Collections"></param>
  142. /// <returns></returns>
  143. public string GetCollections(NameValueCollection Collections)
  144. {
  145. string Parameters = string.Empty;
  146. if (Collections == null || Collections.Count == 0)
  147. {
  148. return Parameters;
  149. }
  150. foreach (string key in Collections.Keys)
  151. {
  152. Parameters += string.Format("{0}={1}&", key, Collections[key]);
  153. }
  154. if (!string.IsNullOrWhiteSpace(Parameters) && Parameters.EndsWith("&"))
  155. {
  156. Parameters = Parameters.Substring(0, Parameters.Length - 1);
  157. }
  158. return Parameters;
  159. }
  160. }
  161. }