Question

I want to add some custom plugins to glimpse. However the profiling I want to do is expensive so I only want to do it if glimpse id turned on for the current request. Can I access a property in code to tell me if glimpse is currently on.

Something like:

if(GlimpseConfig.IsGlimpseActive)
{

} 
Was it helpful?

Solution

First of all if you're creating a plugin for Glimpse like a custom tab then your tab will only be asked for data if Glimpse is active for the given request.

So to be clear, if you want to make a check like you mentioned above inside your tab, then that is not necessary since Glimpse will not call you in the first place. But if you are talking about enabling/disabling some profiling code that will be accessed by your custom tab and which might be expensive, then I guess a check might indeed by helpful. Unfortunately there is currently no way to do this without some kind of abuse of Glimpse internals.

Your question seems to have the same requirement as this question so I'm not going to paste the same answer here, but I'll paste the code sample for completeness of this answer.

But in short you could do the following if you keep in mind that there are no guarantees on whether this will continue to work in upcoming releases, but check the other question for more details.

public static class CurrentGlimpseStatus
{
    private const string GlimpseCurrentRuntimePolicyKey = "__GlimpseRequestRuntimePermissions";

    public static bool IsEnabled
    {
        get
        {
            RuntimePolicy currentRuntimePolicy = RuntimePolicy.Off;
            if (System.Web.HttpContext.Current.Items.Contains(GlimpseCurrentRuntimePolicyKey))
            {
                currentRuntimePolicy = (RuntimePolicy)System.Web.HttpContext.Current.Items[GlimpseCurrentRuntimePolicyKey];
            }

            return !currentRuntimePolicy.HasFlag(RuntimePolicy.Off);
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top