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);
}
}
}
///
/// 监控日志对象
///
public class MonitorLog
{
public string ControllerName
{
get;
set;
}
public string ActionName
{
get;
set;
}
public DateTime ExecuteStartTime
{
get;
set;
}
public DateTime ExecuteEndTime
{
get;
set;
}
///
/// Form 表单数据
///
public NameValueCollection FormCollections
{
get;
set;
}
///
/// URL 参数
///
public NameValueCollection QueryCollections
{
get;
set;
}
///
/// 监控类型
///
public enum MonitorType
{
Action = 1,
View = 2
}
///
/// 获取监控指标日志
///
///
///
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);
}
///
/// 获取Post 或Get 参数
///
///
///
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;
}
}
}