Вопрос

I'm using Glimpse.ADO to profile Linq to SQL commands using the following code:

var connection = new SqlConnection(connectionString);
var conn = new GlimpseDbConnection(connection);
context = new ApplicationDatabaseDataContext(conn, mappingSource);

The above code works fine and I can see SQL queries in the HUD.

I would like to disable Glimpse in production so I'm using the following code in the web.config

<glimpse defaultRuntimePolicy="Off">

However, I'd like to remove the GlimpseDbConnection to prevent any unnecessary performance impact on monitoring each query. Ideally I could do something like:

if (Glimpse.Enabled)
{
    var connection = new SqlConnection(connectionString);
    var conn = new GlimpseDbConnection(connection);
    context = new ApplicationDatabaseDataContext(conn, mappingSource);
}
else
{
    context = new ApplicationDatabaseDataContext(connectionString, mappingSource);
}

Obviously Glimpse.Enabled doesn't exist, but is there a way I can do something similar?

Это было полезно?

Решение

In all honesty, I wouldn't recommend checking to see if Glimpse is enabled (in this situation).

Instead, leverage ADO.NET's DbProviderFactories, which Glimpse can hook into transparently.

Here's an example:

var connectionString = ConfigurationManager.ConnectionStrings["YourConnectionString"];
var factory = DbProviderFactories.GetFactory(connectionString.ProviderName);  

using (DbCommand cmd = factory.CreateCommand())
{ 
    // work with cmd
    using (DbConnection con = factory.CreateConnection())
    {
        // work with con
    } 
}

Glimpse will automatically work with this code when enabled, and automatically stay out of the way when disabled - the best of both worlds!

Другие советы

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);
        }
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top