Click here to Skip to main content
15,867,568 members
Articles / Profiling

MiniProfiler - Installation and Setup

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
27 Dec 2012CPOL3 min read 35.7K   6   4
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:
    VB.NET
    "Install-Package MiniProfiler.EF"
  5. Now open your Global.asax.vb file, at the top of your Sub Application_Start, add:
    VB.NET
    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:
    VB.NET
    MiniProfiler.Start()
  7. And at the top of your Application_EndRequest:
    VB.NET
    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:

VB.NET
<%:MiniProfiler.RenderIncludes %>

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

VB.NET
<%@ 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.

VB.NET
<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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Hewlett Packard Enterprise Security Services
United Kingdom United Kingdom
Technical Architect for Hewlett-Packard Enterprise Security Service.

Please take the time to visit my site

Comments and Discussions

 
QuestionMiniProfiler + Log4Net Pin
Thang Believe4-Jun-14 12:59
Thang Believe4-Jun-14 12:59 
QuestionNice copy and paste article Pin
Member 1051652731-Jan-14 7:58
Member 1051652731-Jan-14 7:58 
AnswerRe: Nice copy and paste article Pin
Karl Stoney3-Feb-14 9:03
Karl Stoney3-Feb-14 9:03 
QuestionNice Article Pin
JoCodes19-Nov-13 18:58
JoCodes19-Nov-13 18:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.