Using MiniProfiler with a hybrid asp.net Web Forms / MVC application - Part 3

Previous parts:

Controlling when MiniProfiler is visible

More often than not, you won't want MiniProfiler to be running / showing its user interface when you're running in production. However, it can be very useful to be able to trigger MiniProfiler on a production instance of your application so that you can see what's going on, along with all the other diagnostics and logging that you've got setup. A fairly simple way to do this is to have MiniProfiler enable/disable itself based on a trigger from your application running in the browser. 

One way to do this is to make the call to MiniProfiler.Start in Global.asax dependent on the presence of a cookie:

protected void Application_BeginRequest()
{
    if (HttpContext.Current.Request.Cookies.AllKeys.Contains("EnableMiniProfiler"))
    {
        MiniProfiler.Start();
    }
}

Once you've done this (You'd probably also follow the example at http://miniprofiler.com/ where the Application_BeginRequest method checks Request.IsLocal and turns on profiling if that's true, i.e. if the request is local OR the cookie is set, call MiniProfiler.Start()) the easiest way to "light this up" in the client side code is to install the jQuery.Cookie NuGet package and drop in a couple of JavaScript methods, along with a script reference to jQuery.cookie:

function EnableProfiling()
{
    $.cookie("EnableMiniProfiler", 1, { expires: 1 });
}
function DisableProfiling()
{
    $.removeCookie("EnableMiniProfiler");
}

With this in place, loading your application, opening the browsers developer tools and calling the EnableProfiling method means that subsequently hitting F5 will cause the MiniProfiler UI to display and diagnostic profiling information to be displayed.

About Rob

I've been interested in computing since the day my Dad purchased his first business PC (an Amstrad PC 1640 for anyone interested) which introduced me to MS-DOS batch programming and BASIC.

My skillset has matured somewhat since then, which you'll probably see from the posts here. You can read a bit more about me on the about page of the site, or check out some of the other posts on my areas of interest.

No Comments

Add a Comment