Using MiniProfiler with a hybrid asp.net Web Forms / MVC application - Part 3
Previous parts:
- Part 1 - Installing MiniProfiler and showing the UI
- Part 2 - Hooking MiniProfiler into an MVC controller
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.