先决条件使用Controller页面。
1、核心MVCAttribute监控信息如下:
a)HttpMethod:poset\get\head
b)ActionName:不解释了。
c)ControllerName
d)ControllerNamespace
e)TimeCost:耗时
f)ActionParameters
其实随便在改改这里完全可以实现无埋点的另一种实现方式。只不过这个依托于C#而已。
2、Global.asax.cs在加入如下代码即可 System.Web.Mvc.GlobalFilters.Filters.Add(new MVCAttribute());
3、如需去掉监控在方法名或者类名上加[NoMVC]即可,如下所示:仅仅移除对Get方法的监视。
socket 配套使用:http://blog.csdn.net/gzy11/article/details/78340944
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
[NoMVC]
public string Get(int id)
{
return "value";
}
// POST api/values
public void Post([FromBody]string value)
{
}
// PUT api/values/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/values/5
public int Delete(int id)
{
return id;
}
}
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
//GlobalConfiguration.Configuration.Filters.Add(new WepAPIAttribute());
//GlobalConfiguration.Configuration.Filters.Add(new MVCFilters());
//System.Web.Mvc.GlobalFilters.Filters.Add(new MVCFilters());
//System.Web.Mvc.GlobalFilters.Filters.Add(new MVCFilters());
System.Web.Mvc.GlobalFilters.Filters.Add(new MVCAttribute());
}
}
public class MVCAttribute : System.Web.Mvc.ActionFilterAttribute
{
private const string Key = "__action_duration__";
private const string ParametersKey = "__action_Parameters__";
// 摘要:
// 在执行操作方法后由 ASP.NET MVC 框架调用。
//
// 参数:
// filterContext:
// 筛选器上下文。
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (filterContext.RequestContext.HttpContext.Items[Key] == null)
{
return;
}
var stopWatch = filterContext.RequestContext.HttpContext.Items[Key] as Stopwatch;
if (stopWatch != null)
{
stopWatch.Stop();
var actionName = filterContext.ActionDescriptor.ActionName;//ActionName;
var controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
var controllerType = filterContext.ActionDescriptor.ControllerDescriptor.ControllerType;
var parameters = filterContext.RequestContext.HttpContext.Items[ParametersKey] as string;
var httpMethod = filterContext.HttpContext.Request.HttpMethod;
//string shortEName = filterContext.HttpContext.Request.Params["ShortEName"];
MVCInfo mcvInfo = new MVCInfo() { HttpMethod = httpMethod, ActionName = actionName, ControllerName = controllerName, ControllerNamespace = controllerType.FullName, TimeCost = stopWatch.ElapsedMilliseconds.ToString(), ActionParameters = parameters};
var socketClient = SocketClient.GetInstance();
string json =SimpleJson.SerializeObject(mcvInfo);
Task task = new Task(() =>
{
socketClient.SendMessage(json);
}); task.Start();
}
}
/// <summary>
/// 获取Action 参数
/// </summary>
/// <param name="collections"></param>
/// <returns></returns>
public string GetCollections(IDictionary<string, object> collections)
{
string parameters = string.Empty;
if (collections == null || collections.Count == 0)
{
return parameters;
}
var allKeys = collections.Keys;
if (allKeys.Count > 0)
{
parameters += "[";
}
foreach (string item in allKeys)
{
var keyValue = collections[item];
string fullName = keyValue == null ? "" : keyValue.GetType().FullName;
string value = keyValue == null ? "" : collections[item].ToString();
parameters += "{\"Name\":\"" + item + "\",\"Value\":\"" + value + "\",\"Type\":\"" + fullName + "\"},";
}
if (allKeys.Count > 0)
{
parameters = parameters.TrimEnd(',');
parameters += "]";
}
return parameters;
}
//
// 摘要:
// 在执行操作方法之前由 ASP.NET MVC 框架调用。
//
// 参数:
// filterContext:
// 筛选器上下文。
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (SkipAction(filterContext))
{
return;
}
var stopWatch = new Stopwatch();
string param = GetCollections(filterContext.ActionParameters);
filterContext.RequestContext.HttpContext.Items[Key] = stopWatch;
filterContext.RequestContext.HttpContext.Items[ParametersKey] = param;
stopWatch.Start();
//base.OnActionExecuting(filterContext);
}
//
// 摘要:
// 在执行操作结果后由 ASP.NET MVC 框架调用。
//
// 参数:
// filterContext:
// 筛选器上下文。
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
//GetMVCResult(filterContext);
//var stopWatch = filterContext.RequestContext.HttpContext.Items[Key] as Stopwatch;
//string spanTime = null;
//if (stopWatch != null)
//{
// spanTime = stopWatch.ElapsedMilliseconds.ToString();
// //contenResult.Content = "耗时:" + spanTime;
// //filterContext.HttpContext.Response.Write("耗时:" + spanTime);
//}
}
//
// 摘要:
// 在执行操作结果之前由 ASP.NET MVC 框架调用。
//
// 参数:
// filterContext:
// 筛选器上下文。
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
}
/// <summary>
/// 获取返回数据
/// </summary>
/// <param name="filterContext"></param>
private void GetMVCResult(ResultExecutedContext filterContext)
{
var queryString = filterContext.HttpContext.Request.QueryString;
var type = filterContext.Result.GetType();
if (type.FullName == "System.Web.Mvc.ContentResult")
{
var contenResult = filterContext.Result as System.Web.Mvc.ContentResult;
string result = contenResult != null ? contenResult.Content : null;
}
else if (type.FullName == "System.Web.Mvc.EmptyResult")
{
var emptyResult = filterContext.Result as System.Web.Mvc.EmptyResult;
string result = emptyResult.ToString();
}
else if (type.FullName == "System.Web.Mvc.FileResult")
{
var fileResult = filterContext.Result as System.Web.Mvc.FileResult;
string result = fileResult != null ? fileResult.FileDownloadName : null;
}
else if (type.FullName == "System.Web.Mvc.HttpUnauthorizedResult")
{
var httpUnauthorizedResult = filterContext.Result as System.Web.Mvc.HttpUnauthorizedResult;
string result = httpUnauthorizedResult != null ? httpUnauthorizedResult.StatusDescription : null;
}
else if (type.FullName == "System.Web.Mvc.JavaScriptResult")
{
var javaScriptResult = filterContext.Result as System.Web.Mvc.JavaScriptResult;
string result = javaScriptResult != null ? javaScriptResult.Script : null;
}
else if (type.FullName == "System.Web.Mvc.JsonResult")
{
var jsonResult = filterContext.Result as System.Web.Mvc.JsonResult;
var result = jsonResult != null ? jsonResult.Data : null;
}
else if (type.FullName == "System.Web.Mvc.RedirectResult")
{
var redirectResult = filterContext.Result as System.Web.Mvc.RedirectResult;
var result = redirectResult != null ? redirectResult.Url : null;
}
else if (type.FullName == "System.Web.Mvc.RedirectToRouteResult")
{
var redirectToRouteResult = filterContext.Result as System.Web.Mvc.RedirectToRouteResult;
var result = redirectToRouteResult != null ? redirectToRouteResult.RouteName : null;
}
else if (type.FullName == "System.Web.Mvc.ViewResultBase")
{
var viewResultBase = filterContext.Result as System.Web.Mvc.ViewResultBase;
var result = viewResultBase != null ? viewResultBase : null;
}
else if (type.FullName == "System.Web.Mvc.ViewResult")
{
//System.Web.Mvc.ViewResult
var viewResult = filterContext.Result as System.Web.Mvc.ViewResult;
var result = viewResult != null ? viewResult : null;
}
}
private static bool SkipAction(ActionExecutingContext filterContext)
{
// return filterContext.ActionDescriptor.GetCustomAttributes(typeof(NoMVCAttribute), true).Any();
filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(typeof(NoMVCAttribute), true);
filterContext.Controller.GetType().GetCustomAttributes(typeof(NoMVCAttribute), true);//.Any();
//return filterContext.ActionDescriptor.GetCustomAttributes<NoLogAttribute>().Any() ||
// filterContext.ControllerContext.ControllerDescriptor.GetCustomAttributes<NoLogAttribute>().Any();
//return filterContext.ActionDescriptor.GetCustomAttributes(typeof(NoMVCAttribute), true).Any();
//if (filterContext.Controller.GetType().GetCustomAttributes(typeof(NoMVCAttribute), true).Any())
//{
// return true;
//}
//if (filterContext.ActionDescriptor.GetCustomAttributes(typeof(NoMVCAttribute), false).Any())
//{
// return false;
//}
var tt = filterContext.ActionDescriptor.GetType();
return filterContext.ActionDescriptor.IsDefined(typeof(NoMVCAttribute), inherit: true) || filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(NoMVCAttribute), inherit: true);
// return filterContext.ActionDescriptor.GetCustomAttributes<NoEarlyWarningAttribute>().Any() || filterContext.ControllerContext.ControllerDescriptor.GetCustomAttributes<NoEarlyWarningAttribute>().Any();
}
}
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true)]
public class NoMVCAttribute : Attribute
{
//使用是在类或方法前加NoMVC
}
}