Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide miniprofiler?

I'm using MVC Mini profiler to check the speed of specific parts of my application, and would like to keep it there just in case something happens later and I may need to check "what's going wrong". It's not a full log set, but it comes pretty in handy to know what's making a page take long.

So, my goal is to hide it and have it profile only when the request comes with a specific parameter. However, none of my attempts have worked in the way that I would expect.

This has done the trick of not showing it on the screen (code in a view):

@if (Request.QueryString.AllKeys.Contains("showProfiler"))
{ 
    @MvcMiniProfiler.MiniProfiler.RenderIncludes()
}

This is the attempt that got closer. Correctly hides the mini profiler info, but at the moment I show it, it profiles everything since I stopped showing it. So, let's say that I profile my page and it takes 3 seconds. I remove the query parameter and load the page three more times. I add my parameter again and I see 4 sets of profile information. That implies that it keeps track of everything and I wonder it if could give memory issues.

Attempts to make that not happen anymore:

Attempt 1:

protected void Application_BeginRequest()
{
    if (Request.QueryString.AllKeys.Contains("showProfiler"))
    {
        MiniProfiler.Start();
    }
}

Attempt 2:

protected void Application_EndRequest()
{
    MiniProfiler.Stop(!Request.QueryString.AllKeys.Contains("showProfiler"));
}

Attempt 3:

protected void Application_EndRequest()
{
    MiniProfiler.Stop(true);
}

None of these worked. Any ideas?

like image 660
Alpha Avatar asked Sep 29 '11 23:09

Alpha


1 Answers

The home page (see the "Abandoning a Profiler Session section) of the profiler has the usage pattern are looking for:

protected void Application_BeginRequest()
{
   MvcMiniProfiler.MiniProfiler.Start();  
}
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
   if(!CurrentUserIsAllowedToSeeProfiler())
   {
       MvcMiniProfiler.MiniProfiler.Stop(discardResults: true);
   }
}

Your implementation of CurrentUserIsAllowedToSeeProfiler will be checking if the query string contains the key the trigger the profiler.


EDIT:

You can also look at their Example Project to see how they implement disabling it in certain situation. Their check is to see if you are accessing it via localhost, but you could of course change that to check the query string instead.

Based on that, it appears that "Attempt #1" should be the trick. Do note that the difference between the one "that is close" and "attempt #1" is the former is looking for query string profiling, whereas your Attempt #1 is checking for showProfiler. Could it have just been a simple query string mixup?

like image 121
vcsjones Avatar answered Nov 15 '22 09:11

vcsjones