2011-06-28 18 views
13

Iv eksperymentował z doskonałym narzędziem, Mvc MiniProfiler.Używanie Miniprofilatora MVC do każdego wywołania akcji

Nie chcę zaśmiecać całego mojego widoku wieloma poleceniami Step, więc chcę używać profilera przy każdym wywołaniu akcji. Kiepski pomysł? Oto, co próbowałem do tej pory:

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

Ale nie sądzę, że to robi to, co zamierzam? Myślę, że muszę uruchomić profiler na OnActionExecuting i zatrzymać go na OnResultExecuted. Jak to zrobić, biorąc pod uwagę, że profiler został zaprojektowany do użycia z instrukcją using.

+1

Jeśli pobierzesz źródło Mini Profiler, to jest to klasa kontrolera podstawowego w przykładowym projekcie, który robi dokładnie to. –

Odpowiedz

24

Można zdefiniować globalne filtr Działanie:

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

i zarejestrować w Global.asax:

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

i to dość dużo wszystko.

+0

Darin czy istnieje odpowiednia metoda dla MVC 2? –

+1

Hah faktycznie musiałem tylko dodać [ProfileActions] do mojego BaseController, to było łatwe. –

+0

jeśli chcesz wiedzieć, który ** kontroler i działanie ** zostało wykonane, użyj 'var step = profiler.Step (" Controller: "+ filterContext.RouteData.Values ​​[" controller "]. ToString() +" Action: "+ filterContext.ActionDescriptor.ActionName); ', nadzieja pomaga komuś. – stom

2

Dobrze jest myśleć i używać ActionFilter. Ale MVC nadal jest aplikacją ASP .NET. Aby uruchomić i zatrzymać program MiniProfiler na początku i końcu każdego żądania, można wypróbować zdarzenia aplikacji w pliku Global.asax.cs.

// -------------------------------------------------------------------------------------------------------------------- 
// <copyright file="Global.asax.cs" company="Believe2014"> 
// http://believeblog.azurewebsites.net/post/miniprofiler--log4net 
// </copyright> 
// <summary> 
// The mvc application. 
// </summary> 
// -------------------------------------------------------------------------------------------------------------------- 

using System; 
using System.Web; 
using System.Web.Http; 
using System.Web.Mvc; 
using System.Web.Optimization; 
using System.Web.Routing; 

namespace Mvc4Application 
{ 
// Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801 

    /// <summary> 
    ///  The mvc application. 
    /// </summary> 
    public class MvcApplication : HttpApplication 
    { 
     /// <summary> 
     ///  The application_ start. 
     /// </summary> 
     protected void Application_Start() 
     { 
      AreaRegistration.RegisterAllAreas(); 

      WebApiConfig.Register(GlobalConfiguration.Configuration); 
      FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
      RouteConfig.RegisterRoutes(RouteTable.Routes); 
      BundleConfig.RegisterBundles(BundleTable.Bundles); 
      AuthConfig.RegisterAuth(); 

      Profiler.Initialize(); 
     } 

     /// <summary> 
     /// The event when the application acquires request state. 
     /// </summary> 
     /// <param name="sender"> 
     /// The sender. 
     /// </param> 
     /// <param name="e"> 
     /// The event argument.. 
     /// </param> 
     protected void Application_AcquireRequestState(object sender, EventArgs e) 
     { 
      Profiler.Start(HttpContext.Current); 
     } 

     /// <summary> 
     /// This function is called by ASP .NET at the end of every http request. 
     /// </summary> 
     /// <param name="sender"> 
     /// The sender. 
     /// </param> 
     /// <param name="e"> 
     /// The event argument. 
     /// </param> 
     protected void Application_EndRequest(object sender, EventArgs e) 
     { 
      Profiler.Stop(); 
     } 
    } 
} 
+0

Wstąpiłem, ponieważ Application_AcquireRequestState okazało się być dobrym miejscem dla mnie do sprawdzenia, czy użytkownik jest uwierzytelniony i ma dostęp do używania/zobacz profilera, ale to nie rozwiązuje dokładnie problemu OP. Tym razem prośba, a nie akcja konkretnie. Drobne szczegóły, ale może być ważne. – DaveD

+0

Istnieje wiele narzędzi, które mogą śledzić czas przetwarzania żądań. Ale ta metoda jest szczególnie ważna w przypadku śledzenia czasu przetwarzania uwierzytelnionych żądań. Opowiada historię każdego użytkownika i jego czasu oczekiwania, a nie tylko ogólne żądania. – Believe2014

Powiązane problemy