本文介绍了禁用Glimpse时删除GlimpseDbConnection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Glimpse.ADO通过以下代码将Linq解析为SQL命令:

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);

上面的代码工作正常,我可以在HUD中看到SQL查询.

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

我想在生产中禁用Glimpse,所以我在web.config中使用以下代码

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

<glimpse defaultRuntimePolicy="Off">

但是,我想删除GlimpseDbConnection以防止对监视每个查询产生任何不必要的性能影响.理想情况下,我可以做类似的事情:

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);
}

显然Glimpse.Enabled不存在,但是有什么办法可以做类似的事情吗?

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

推荐答案

老实说,我不建议您检查是否已启用Glimpse(在这种情况下).

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

相反,请利用ADO.NET的DbProviderFactories,Glimpse可以透明地将其插入.

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

这是一个例子:

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启用后将自动使用此代码,禁用时将自动避开-两全其美!

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

这篇关于禁用Glimpse时删除GlimpseDbConnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-03 20:38