Even if you could do that, you should take into account that Glimpse being enabled or not for a specific request is something that gets reevaluated while your request is traveling through the request processing pipeline. Which means that it is possible that Glimpse is enabled at the beginning of the request, while you make your checks, and that at the end of the request Glimpse gets disabled due to some policy.
But in your case when setting Glimpse disabled in the config, then it will be disabled from the start. Once disabled always disabled for that request, the reverse is not always true.
On the other hand the performance impact is negligible in comparison with the time needed to open the connection and execute the query.
That being said, you can always take the following hacky approach but there are no guarantees that it will keep on working with a new release.
Glimpse stores this current state in the Items collection of the current HttpContext
, so if you can get a hold on the current HttpContext
then you could use the following code to make your conditional check.
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);
}
}
}