Elmah - ASP.NET -> 多个连接字符串 -> 在代码中设置SQL错误日志连接字符串
我希望将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
由于 Elmah 是开源的,因此很容易添加您自己的 :-)
您需要做的就是向
SqlErrorLog
类添加一个新的静态属性,该属性将包含您想要的连接字符串使用,然后您将更新SqlErrorLog.ConnectionString
属性以返回您的自定义连接字符串(如果它有值)。尝试将类似的内容添加到类中(警告 - 未经测试的代码):
这应该足够了。 现在您所要做的就是找到选择连接字符串的位置并将其更新以设置
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 theSqlErrorLog.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):
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.我为此使用了 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.
我将使用一个连接字符串名称“AppDB”,但实际上将连接字符串托管在外部(web.config 文件之外)配置文件中,该文件在每个环境中都不同。
在您的 web.config 中:
然后,在connectionStrings.config 文件中:
然后,根据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:
Then, in the connectionStrings.config file:
Then, depending on what environment the connectionStrings.config file is in, modify the connectionString. In the code, just refer to "AppDB".
为了构建 Rune Grimstad 的答案,除了他的更改之外,还要
添加: after:
在 SqlErrorLog(IDictionary config) 构造函数中
。 然后我做了 RSolberg 所做的事情,但在 application_start 中而不是会话中:
Elmah.SqlErrorLog.CustomConnectionString = "Your CS";
To build on Rune Grimstad's answer, in addition to his changes, add:
after:
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";
在这种情况下我们可以使用数据库上下文吗?
我在 MVC 3 中有一个 web 应用程序,我使用数据库上下文连接到数据库(根据上面的示例):
其中 TESTAppDB:
如果我想连接到另一个数据库,所以我写:
这似乎是正确的...
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):
Where TESTAppDB:
If I want to connect to another database, so I write:
It seems to be correct...