3

我希望将ELMAH集成到现有的 ASP.NET 应用程序中,以进一步支持错误调查,并可以使用连接字符串的一些帮助。我们为部署应用程序的所有环境或我们的环境使用单个 web.config 文件,并且在运行时,应用程序通常根据 URL 决定它所在的环境。

这就是我们想要的标准块...

  <connectionStrings>
    <add name="TESTAppDB" connectionString="Data Source=SQL-T-APPNAME.COMPANY.COM;Initial Catalog=APPNAME;User ID=USER;Password=THEPASS" providerName="System.Data.SqlClient"/>
    <add name="CERTAppDB" connectionString="Data Source=SQL-C-APPNAME.COMPANY.COM;Initial Catalog=APPNAME;User ID=USER;Password=THEPASS" providerName="System.Data.SqlClient"/>
    <add name="PRODAppDB" connectionString="Data Source=SQL-P-APPNAME.COMPANY.COM;Initial Catalog=APPNAME;User ID=USER;Password=THEPASS" providerName="System.Data.SqlClient"/>
  </connectionStrings>

使用 Elmah,您似乎只需要指定连接字符串的名称,但是如何在运行时动态地执行此操作?例如,如果我正在测试,那么我想要这个:

<elmah>
     <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="TESTAppDB"/>
</elmah>

但如果我在 PROD:

<elmah>
     <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="PRODAppDB"/>
</elmah>

编辑
Web 应用程序的部署实践远远超出了我想要做的范围。我需要一个代码解决方案,允许我更改 ELMAH Sql 错误日志的数据源...

我无法改变我们今天部署 Web 应用程序的方式。这意味着,无论 TEST 是什么,都会转移到 CERT。CERT 中的内容移至 PROD。Web 应用程序必须能够确定它所在的环境并运行...

4

5 回答 5

4

我为此使用了一个 Web 部署项目。您可以为每个配置指定一个包含连接字符串的文件(默认为调试和发布)。这样,所有部署的所有配置文件都具有相同的文件,并且您的 web.config 会针对每个部署进行适当的更新。

Scott Gu 在这里有很好的信息:http ://weblogs.asp.net/scottgu/archive/2005/11/06/429723.aspx

它适用于以前的版本,但信息仍然适用。

于 2009-05-18T18:17:26.053 回答
4

由于 Elmah 是开源的,因此很容易添加您自己的 :-)

您需要做的就是向SqlErrorLog包含您要使用的连接字符串的类添加一个新的静态属性,然后您将更新该SqlErrorLog.ConnectionString属性以返回您的自定义连接字符串(如果它有一个值)。

尝试在类中添加这样的内容(警告 - 未经测试的代码):

public static string CustomConnectionString { get; set; }

public virtual string ConnectionString
{
  get 
  { 
    if (string.IsNullOrEmpty(CustomConnectionString)) 
    {
      return _connectionString;
    }
    else
    {
      return CustomConnectionString; 
    }
  }
}

这应该足够了。现在您所要做的就是找到您选择连接字符串的位置并更新它以设置SqlErrorLog.CustomConnectionString属性。

于 2009-05-18T18:42:54.753 回答
0

我会使用一个连接字符串名称“AppDB”,但实际上将连接字符串托管在一个外部(web.config 文件之外)配置文件中,这在每个环境中都不同。

在您的 web.config 中:

<connectionStrings configSource="config\connectionStrings.config"/>

然后,在 connectionStrings.config 文件中:

<connectionStrings>    
    <add name="AppDB" connectionString="Data Source=SQL-T-APPNAME.COMPANY.COM;Initial Catalog=APPNAME;User ID=USER;Password=THEPASS" providerName="System.Data.SqlClient"/>
</connectionStrings>

然后根据connectionStrings.config文件所在的环境,修改connectionString。在代码中,只需参考“AppDB”即可。

于 2009-05-18T18:07:48.943 回答
0

为了建立 Rune Grimstad 的答案,除了他的更改,添加:

if (connectionString.Length == 0)
    connectionString = CustomConnectionString;

后:

string connectionString = ConnectionStringHelper.GetConnectionString(config);

在 SqlErrorLog(IDictionary config) 构造函数中。

然后我做了 RSolberg 所做的事情,但是在 application_start 中,而不是在 session 中:

Elmah.SqlErrorLog.CustomConnectionString = "你的 CS";

于 2010-06-24T21:05:45.310 回答
0

我们可以在这种情况下使用数据库上下文吗?

我在 MVC 3 中有一个 webapp,我使用数据库上下文连接到数据库(根据上面的示例):

using (var ctx = new TESTAppDB())
{
    var res = (from p in ctx.Customer select p).FirstOrDefault();
}

其中 TESTAppDB:

public class TESTAppDB: DbContext
{
    public DbSet<Customer> Customer{ get; set; }
}

如果我想连接到另一个数据库,所以我写:

using (var ctx = new CERTAppDB())
{
    var res = (from p in ctx.Order select p).FirstOrDefault();
}

好像是对的……

于 2011-11-23T13:50:39.100 回答