• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

asp.net mvc 获取页面执行时间的类

C# 水墨上仙 2448次浏览

asp.net mvc 获取页面执行时间的类
转自:http://blog.csdn.net/keepitshortandsimple/article/details/7357954

1)&nbsp在我们的项目中添加一个类文件,

[csharp] view plaincopy
public class PerformanceActionAttributeFilter : ActionFilterAttribute  
{  
    public string Message { get; set; }  
  
    public override void OnActionExecuted(ActionExecutedContext filterContext)  
    {  
        //在Action执行之后执行 输出到输出流中文字:After Action execute xxx  
        //filterContext.HttpContext.Response.Write(@"<br />After Action execute" + "\t " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fffff"));  
  
        GetTimer(filterContext, "action").Stop();  
  
        base.OnActionExecuted(filterContext);  
    }  
  
    public override void OnActionExecuting(ActionExecutingContext filterContext)  
    {  
        //在Action执行前执行  
        //filterContext.HttpContext.Response.Write(@"<br />Before Action execute" + "\t " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fffff"));  
  
        GetTimer(filterContext, "action").Start();  
  
        base.OnActionExecuting(filterContext);  
    }  
  
    public override void OnResultExecuted(ResultExecutedContext filterContext)  
    {  
        //在Result执行之后  
        //filterContext.HttpContext.Response.Write(@"<br />After ViewResult execute" + "\t " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fffff"));  
  
        var renderTimer = GetTimer(filterContext, "render");  
        renderTimer.Stop();  
  
        var actionTimer = GetTimer(filterContext, "action");  
        var response = filterContext.HttpContext.Response;  
  
        if (response.ContentType == "text/html")  
        {  
            response.Write(  
                String.Format(  
                    "<p>Action '{0} :: {1}', Execute: {2}ms, Render: {3}ms.</p>",  
                    filterContext.RouteData.Values["controller"],  
                    filterContext.RouteData.Values["action"],  
                    actionTimer.ElapsedMilliseconds,  
                    renderTimer.ElapsedMilliseconds  
                )  
            );  
        }  
  
        base.OnResultExecuted(filterContext);  
    }  
  
    public override void OnResultExecuting(ResultExecutingContext filterContext)  
    {  
        GetTimer(filterContext, "render").Start();  
  
        base.OnResultExecuting(filterContext);  
    }  
  
    private Stopwatch GetTimer(ControllerContext context, string name)  
    {  
        string key = "__timer__" + name;  
        if (context.HttpContext.Items.Contains(key))  
        {  
            return (Stopwatch)context.HttpContext.Items[key];  
        }  
  
        var result = new Stopwatch();  
        context.HttpContext.Items[key] = result;  
        return result;  
    }  
  
}  

2)在Controller中添加描述

[PerformanceActionAttributeFilter(Message ="controller")]  
public class HomeController : Controller  
{  
    [PerformanceActionAttributeFilter(Message = "action")]  
    public ActionResult Index()  
    {     
        return View();  
    }  
  
    public ActionResult About()  
    {  
        return View();  
    }  
}  


喜欢 (0)
加载中……