用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - asp代码库

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

2014-12-18 作者: asp源码之家举报

[asp]代码库

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;  
    }  

}  



网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...