Elmah - ASP.NET -> 多个连接字符串 -> 在代码中设置SQL错误日志连接字符串

发布于 2024-07-19 10:52:41 字数 1469 浏览 6 评论 0原文

我希望将 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>

但如果我处于生产阶段:

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

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

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

I am looking to integrate ELMAH into an existing ASP.NET application to further support error investigations and could use some help with the connection strings. We use a single web.config file for all or our environments the application is deployed in, and at runtime, the app decides which environment it is in, typically based on URL.

This is what a standard block would like like for us...

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

With Elmah, it appears that you just need to specify the name of the connection string, but how can I do this dynamically at runtime? For example, if I'm test, then I want this:

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

but if I'm in PROD:

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

EDIT
The deployment practices for web applications are well out of scope for what I am trying to do. I need a code solution that allows me to change the datasource for the ELMAH Sql Error Log...

I cannot change the way we deploy web apps today. That means, whatever is in TEST, moves to CERT. What is in CERT moves to PROD. The web app must be able to determine which environment it is in and run as such...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

北斗星光 2024-07-26 10:52:41

由于 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 属性。

Since Elmah is open-source it would be easy to add your own :-)

All you would need to do was to add a new static property to the SqlErrorLog class that would contain the connection string you wanted to use and then you would update the SqlErrorLog.ConnectionString property to return your custom connection string if it had a value.

Try to add something like this to the class (warning - untested code):

public static string CustomConnectionString { get; set; }

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

This should be enough. Now all you must do is to find the location where you select your connection string and update it to also set the SqlErrorLog.CustomConnectionString property.

淡紫姑娘! 2024-07-26 10:52:41

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

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

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

I use a Web Deployment Project for this. You can specify a file with the connection string in it for each Configuration (Debug and Release by default). This way all your config files for all deployments have the same files and your web.config is updated appropriately for each deployment.

Scott Gu has good info here: http://weblogs.asp.net/scottgu/archive/2005/11/06/429723.aspx

It is for a previous release but the information still applies.

丶视觉 2024-07-26 10:52:41

我将使用一个连接字符串名称“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”即可。

I would use one connection string name "AppDB", but actually host the connection string in an external (outside of web.config file) config file, that is different in each environment.

In your web.config:

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

Then, in the connectionStrings.config file:

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

Then, depending on what environment the connectionStrings.config file is in, modify the connectionString. In the code, just refer to "AppDB".

假装不在乎 2024-07-26 10:52:41

为了构建 Rune Grimstad 的答案,除了他的更改之外,还要

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

添加: after:

string connectionString = ConnectionStringHelper.GetConnectionString(config);

在 SqlErrorLog(IDictionary config) 构造函数中

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

Elmah.SqlErrorLog.CustomConnectionString = "Your CS";

To build on Rune Grimstad's answer, in addition to his changes, add:

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

after:

string connectionString = ConnectionStringHelper.GetConnectionString(config);

in the SqlErrorLog(IDictionary config) constructor.

Then I did what RSolberg did, but in application_start as opposed to session:

Elmah.SqlErrorLog.CustomConnectionString = "Your CS";

溺深海 2024-07-26 10:52:41

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

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

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

这似乎是正确的...

Can we use in this case Database Context?

I have a webapp in MVC 3 and I connect to database with database context (according to above example):

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

Where TESTAppDB:

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

If I want to connect to another database, so I write:

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

It seems to be correct...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文