您可以从web.config文件中的其他位置拉出log4net AdoNetAppender的connectionString吗? [英] Can you pull the connectionString for a log4net AdoNetAppender from elsewhere in a web.config file?

查看:61
本文介绍了您可以从web.config文件中的其他位置拉出log4net AdoNetAppender的connectionString吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的web.config文件中已经有一个数据库连接字符串.我扫描了log4net文档,但似乎无法在web.config文件的log4net部分中找到使用它的方法.可以做这样的事情吗?

I already have a db connection string in my web.config file. I scanned the log4net docs, but can't seem to find a way to use it within the log4net section of my web.config file. Is is possible to do something like this?

<connectionStrings>
    <add name="connStr" connectionString="Data Source=localhost; ..." />
</connectionStrings>

<log4net>
    <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
    <connectionString connectionStringName="connStr"/>
      ...
</log4net>

推荐答案

创建一个扩展AdoNetAppender的类-例如WebAppAdoNetAppender.在该类中实现ConnectionString属性,然后从该属性设置器中的web.config文件中检索连接字符串.

Create a class that extends AdoNetAppender - say, WebAppAdoNetAppender. Implement the ConnectionString property in that class, and retrieve the connection string from your web.config file in that property setter.

<log4net>
    <appender name="AdoNetAppender" type="MyApp.WebAppAdoNetAppender">
    ...

...

public class WebAppAdoNetAppender : log4net.Appender.AdoNetAppender
{
    public new string ConnectionString
    {
        get { return base.ConnectionString; }
        set { base.ConnectionString = ...   }
    }
}

这篇关于您可以从web.config文件中的其他位置拉出log4net AdoNetAppender的connectionString吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆