Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using MiniProfiler with MVC 5

Edit Got the answer here

So I wanted to check out MiniProfiler to troubleshoot some performance issues. Before using it on production code I wanted to try it out with the a sample so went ahead with creating a MVC 5 application. This is plain vanilla app that gets created with the template.

Added this code in the Index() method of HomeController:

var profiler = MiniProfiler.Current;
        using (profiler.Step("Set page title"))
        {
            ViewBag.Title = "Home Page";
        }
        using (profiler.Step("Doing complex stuff"))
        {
            using (profiler.Step("Step A"))
            { // something more interesting here
                Thread.Sleep(100);
            }
            using (profiler.Step("Step B"))
            { // and here
                Thread.Sleep(250);
            }
        }
        return View();

Added this line below the jquery bundle in _Layout:

@Scripts.Render("~/bundles/jquery")
@StackExchange.Profiling.MiniProfiler.RenderIncludes()

@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)

Ran the app. Nothing shows up. No profiling, nothing.

What am I missing?

Regards.

like image 842
Codehelp Avatar asked Mar 29 '14 05:03

Codehelp


People also ask

How do I install MiniProfiler for MVC?

Always install a specific version when working with ASP.NET MVC application. For example, if working with ASP.NET MVC 4, then choose “MiniProfiler.Mvc4” from NuGet and install it or if working with ASP.NET MVC 3, then choose “MiniProfiler.Mvc3” from NuGet and install it.

What is MiniProfiler and how to use it?

MiniProfiler is an open source profiling library which monitors the performance of a .NET application. It is very lightweight and fast. Using this, we can easily track a performance issue with our application. It was created by Stack Overflow Team members. To know more about MiniProfiler, just visit the official site. Why is MiniProfiler useful?

How to measure query performance with MiniProfiler Entity Framework?

MiniProfiler helps you to measure perfomance of your applications. With Entity Framework extension you will be able to measure query performance.” First you need to install the MiniProfiler package and MiniProfiler Entity Framework package. (I assume you already created an ASP.NET Core Web API project, if not create it first.)

How to set the default path for MiniProfiler?

For MiniProfiler the default path is '/mini-profiler-resources/results', but we have an option to override 'mini-profiler-resources' part of the path using the 'RouteBasePath' configuration property. return request.Query["enableProfiler"].ToString() != string.Empty &&


2 Answers

This is what I had to do to get MiniProfiler working in my ASP.NET MVC5 project:

  1. Installed the MiniProfiler and MiniProfiler.MVC4 NuGet packages (the MVC4 package supports MVC5)

  2. Add the following to Application_Start() in Global.asax:

    protected void Application_Start()
    {
        ...
        // Setup profiler for Controllers via a Global ActionFilter
        GlobalFilters.Filters.Add(new ProfilingActionFilter());
    
        // initialize automatic view profiling
        var copy = ViewEngines.Engines.ToList();
        ViewEngines.Engines.Clear();
        foreach (var item in copy)
        {
            ViewEngines.Engines.Add(new ProfilingViewEngine(item));
        }
    }
    
  3. Add the following to 'Application_BeginRequest()' and 'Application_EndRequest()', also in Global.asax:

    protected void Application_BeginRequest()
    {
        if (Request.IsLocal)
        {
            MiniProfiler.Start();
        }
    }
    
    protected void Application_EndRequest()
    {
        MiniProfiler.Stop();
    }
    
  4. Add the following to _Layout.cshtml (just before the </body> tag):

        ...
        @StackExchange.Profiling.MiniProfiler.RenderIncludes()
    </body>
    </html>
    
  5. Add the following to the <handlers> section of Web.config:

    <system.webServer>
        ...
        <handlers>
            ...
            <add name="MiniProfiler" path="mini-profiler-resources/*" verb="*"
                 type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified"
                 preCondition="integratedMode" />
            ...
        </handlers>
    </system.webServer>
    

That was enough to profile each of the MVC Controller Actions and Views.


In my particular project I was using Entity Framework 6, so I also did the following:

a) Installed the MiniProfiler.EF6 package

b) Added the following to the end of Application_Start() in Global.asax:

    ...
    MiniProfilerEF6.Initialize();
}
like image 177
DigitalDan Avatar answered Oct 20 '22 17:10

DigitalDan


Also you have to add call:

MiniProfiler.Start();

In Global.asax.cs to Application_BeginRequest event.

And:

MiniProfiler.Stop();

In Global.asax.cs to Application_EndRequest event.

like image 21
Ivan Doroshenko Avatar answered Oct 20 '22 17:10

Ivan Doroshenko