Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

MiniProfiler - Installation and Setup

0.00/5 (No votes)
27 Dec 2012 2  
This .NET Tutorial will tell you how to Install and Configure MiniProfiler for .NET 4.5 MVC 4 Web Application including Entity Framework logging.

Overview

The primary reason I started this blog was to share with you the things I come across in my day job as a Web Developer. The first batch of articles I am going to write will be around the tools I find invaluable in my role.

So first and foremost, let’s take a look at MiniProfiler.

MiniProfiler is a tool created and used by the StackExchange group of websites and is used for profiling your .NET and Ruby applications.

Whilst in your development environment, you can use it to get an overlay on your page detailing the code execution of your Application. For those of you that have ever tried to identify bottlenecks in your applications, you can immediately see how useful this can be. Take a look at this screenshot from the Jambr blog page.

MiniProfiler Overlay

As you can see, the steps needed to render my page, and most importantly the SQL that was executed in the process are clearly detailed out here, you can even click on the query to view the detail, for example:

MiniProfiler SQL Dump

I’ll go into all of this a little more in later articles, but for now, let’s look at setting up and configuring MiniProfiler against a .NET 4.5 MVC 4 Application which uses Entity Framework 5 for its database connections.

Installation

Installation is simple and is all done through the Package Manager Console integrated into Visual Studio.

  1. Open the Package Manager Console (Tools -> Library Package Manager -> Package Manager Console).
  2. Type “Install-Package MiniProfiler” and press enter:

    MiniProfiler Package Manager

  3. Package Manager will now download and install MiniProfiler and add the required references to your project.
  4. As I mentioned previously, we’re going to be configuring MiniProfiler to profile our Entity Framework database context, in order to do so, we’ll also need the MiniProfiler.EF package, which is installed in exactly the same way, with:
    “Install-Package MiniProfiler.EF”
  5. Now open your Global.asax.vb file, at the top of your Sub Application_Start, add:
    MiniProfilerEF.Initialize_EF42()
    This ensures that any connections created and used by Entity Framework are captured and logged into MiniProfiler like in my screenshot above.
  6. In the same file, add the following to the top of your Application_BeginRequest:
    MiniProfiler.Start()
  7. And at the top of your Application_EndRequest:
    MiniProfiler.Stop()

Almost done, I promise. We now need to add a piece of code to the page you want to see the profiler on. Personally, I have this on my Base.Master, the parent master page for all others, that way, it’s available on every page. The code you need to add, in your <head> tag is:

<%:MiniProfiler.RenderIncludes %>

You may need to add a reference to the namespace at the top of your page like so:

<%@ Import Namespace="StackExchange.Profiling" %>

And that’s it! It’s worth noting that if MiniProfiler.Start() isn’t called, the .RenderIncludes function is smart enough not to clutter your browser with the JavaScript files that are required. I personally wrap my .Start() in a Debugger.IsAttached statement, therefore I only show the profile in my local development environment.

Run your application and see what happens…

"Oh wait, Karl, you lied – it doesn’t work and I’m getting the following error when running MVC4."

Failed to load resource: the server responded with a status of 404 (Not Found) http://*/mini-profiler-resources/results

This article wouldn’t be interesting unless it solved at least one annoyance now, would it? To fix the above error, you need to add the following to your Web.Config.

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

This sorts out the routing issue which appeared in MVC4. Now try again, and you’ll be sorted!

Summary

MiniProfiler is a very powerful tool, I will go into the more advanced features of it in a later article.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here