使用MVC Miniprofiler每一个行动呼吁 [英] Using MVC Miniprofiler for every action call

查看:77
本文介绍了使用MVC Miniprofiler每一个行动呼吁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

㈣已经试验了伟大的工具, MiniProfiler 。

Iv been experimenting the great tool, Mvc MiniProfiler.

我不希望我的垃圾有许多步骤命令的所有观点,所以我想使用Profiler与每一个行动呼吁。馊主意?这是我到目前为止已经试过:

I don't want to litter all my view with lots of Step commands, so I am wanting to use the profiler with every action call. Bad idea? This is what I have tried so far:

 public abstract class BaseController : Controller
 {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var profiler = MiniProfiler.Current;
            using (profiler.Step("Action: "+filterContext.ActionDescriptor.ActionName))
            {
                base.OnActionExecuting(filterContext);
            }
        }
}

但我不认为这是做什么我打算?我想我需要开始 OnActionExecuting 剖析,并停止它 OnResultExecuted 。我如何做到这一点,考虑到分析器的设计与使用语句中使用。

But I don't think this is doing what I am intending? I think I need to start the profiler on OnActionExecuting and stop it on OnResultExecuted. How do I do this, considering the profiler is designed to be used with the using statement.

推荐答案

您可以定义一个全球行动过滤器:

You could define a global action filter:

public class ProfileActionsAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var profiler = MiniProfiler.Current;
        var step = profiler.Step("Action: " + filterContext.ActionDescriptor.ActionName);
        filterContext.HttpContext.Items["step"] = step;
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        var step = filterContext.HttpContext.Items["step"] as IDisposable;
        if (step != null)
        {
            step.Dispose();
        }
    }
}

和在注册的Global.asax

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
    filters.Add(new ProfileActionsAttribute());
}

这就是pretty所有得多

and that's pretty much all.

这篇关于使用MVC Miniprofiler每一个行动呼吁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆