Using MVC Miniprofiler for every action call

前端 未结 2 1096
别跟我提以往
别跟我提以往 2021-02-02 00:10

Iv been experimenting the great tool, Mvc MiniProfiler.

I don\'t want to litter all my view with lots of Step commands, so I am wanting to use the profiler

2条回答
  •  旧巷少年郎
    2021-02-02 00:50

    It is alright to think and use ActionFilter. But MVC is still an ASP .NET application. To start and stop MiniProfiler at beginning and ending of each request, you can try the application events in Global.asax.cs file.

    // --------------------------------------------------------------------------------------------------------------------
    // 
    //   http://believeblog.azurewebsites.net/post/miniprofiler--log4net
    // 
    // 
    //   The mvc application.
    // 
    // --------------------------------------------------------------------------------------------------------------------
    
    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
    
        /// 
        ///     The mvc application.
        /// 
        public class MvcApplication : HttpApplication
        {
            /// 
            ///     The application_ start.
            /// 
            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();
            }
    
            /// 
            /// The event when the application acquires request state.
            /// 
            /// 
            /// The sender.
            /// 
            /// 
            /// The event argument..
            /// 
            protected void Application_AcquireRequestState(object sender, EventArgs e)
            {
                Profiler.Start(HttpContext.Current);
            }
    
            /// 
            /// This function is called by ASP .NET at the end of every http request.
            /// 
            /// 
            /// The sender.
            /// 
            /// 
            /// The event argument.
            /// 
            protected void Application_EndRequest(object sender, EventArgs e)
            {
                Profiler.Stop();
            }
        }
    }
    

提交回复
热议问题