我制作了自己的 HttpModule,名为“JsonLogger”,用于记录 json 请求和响应。
对于日志记录,我需要一个连接字符串。
为了找到连接字符串,我在 web.config 中添加了一个,然后通过代码获取它。
网络配置
<connectionStrings>
<add name="WebShopConfiguration" connectionString="myConString"/>
</connectionStrings>
<system.web>
<httpModules>
<add name="JsonLogger" type="ServiceModel.Logging.JsonLogger, ServiceModel"/>
</httpModules>
</system.web>
<system.webServer>
<modules>
<add name="JsonLogger" type="ServiceModel.Logging.JsonLogger, ServiceModel"/>
</modules>
</system.webServer>
Http模块
ConnectionString = System.Configuration.ConfigurationManager.
ConnectionStrings["WebShopConfiguration"].ConnectionString;
一切正常,但 imo 这并不干净(未来的开发人员不会知道为什么我的 web.config 中有那个连接字符串)。
我想填写我在 web.config 中添加 httpmodule 的连接字符串。我试过了,但这不起作用(就像我已经想的那样):
网络配置
<add name="JsonLogger" type="ServiceModel.Logging.JsonLogger, ServiceModel" ConnectionString="myConString"/>
HttpModule 中的属性
[ConfigurationProperty("ConnectionString", DefaultValue = "", IsRequired = true, IsKey = false)]
public string ConnectionString { get; set; }
经过一番研究,我仍然没有找到一种方法来做到这一点。任何人都知道我想要实现的目标是否可行?
非常感谢,凯尔