3

I have installed Glimpse (Glimpse MVC4) and MiniProfiler (with EF support).

I also installed the MiniProfiler plugin for Glimpse.

I have that all wired up and working. I want to allow the configuration of Glimpse to determine if MiniProfiler should start profiling. That is, if Glimpse is enabled (through Glimpse.axd not via a config setting) I want to call MiniProfiler.Start() in the Application_BeginRequest() method. So, something like this:

protected void Application_BeginRequest()
{
    if (Glimpse.IsRunning)
    {
        MiniProfiler.Start();
    }
}

Is there a way to determine if Glimpse is enabled?

Paul
  • 1,129
  • 2
  • 17
  • 37

1 Answers1

5

Technically there is a way, but I'd call it hacky at best. I'll let you decide if it is a good fit for your purposes.

var policyString = HttpContext.Current.Items["__GlimpseRequestRuntimePermissions"].ToString();
RuntimePolicy glimpsePolicy;
RuntimePolicy.TryParse(policyString, out glimpsePolicy);

if (!glimpsePolicy.HasFlag(RuntimePolicy.Off))
{
    MiniProfiler.Start();
}

The reason I call it a hack is because while Glimpse may be On at the beginning of request, it may be later turned Off.

An example of this behavior is when Glimpse automatically shuts off once ASP.NET begins to report an unsupported media type, like an image. ASP.NET does not have the ability to know the media type until after the HTTP Handler has run. In this case, Glimpse will say that it is on at the beginning of the request, but then will be off at the end of it.

Nick Larsen
  • 18,631
  • 6
  • 67
  • 96
nikmd23
  • 9,095
  • 4
  • 42
  • 57
  • 2
    With MiniProfiler you can start recording timing information for each request and then choose to keep it or destroy it at the end of the request using `MiniProfiler.Stop(discardResults: someValueThatKnowsIfGlimpseWasTurnedOffDuringTheRequest);`. – Nick Larsen Mar 25 '13 at 15:32
  • Perfect! You can get `someValueThatKnowsIfGlimpseWasTurnedOffDuringTheRequest` from Glimpse the exact same way, just do it in `Application_EndRequest`. – nikmd23 Mar 25 '13 at 16:13
  • I guess my concern is, I don't want the overhead of MiniProfiler running at all. I want to use Glimpse's on/off status to enable total debugging...While the site is on the production server. Granted the site won't see *that* much traffic, but I'd like to save the overhead nonetheless. – Paul Mar 25 '13 at 18:32
  • Actually...I get it now. This solution will work...Thanks for the information. I also noticed that there is a cookie called "glimpsePolicy" and it is set to "On" can that be relied upon? – Paul Mar 25 '13 at 18:44
  • No. The `glimpsePolicy` cookie can be set\overwritten by end users. It's actually how the giant On and off buttons on `Glimpse.axd` work. – nikmd23 Mar 25 '13 at 20:57