Log4NET AdoNetAppender连接字符串参考 [英] Log4NET AdoNetAppender connection string reference

查看:266
本文介绍了Log4NET AdoNetAppender连接字符串参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Log4NET登录到我的数据库,并按照一些教程,我发现我必须为DAO附加程序配置一个连接字符串.

I want to use Log4NET to log into my DB and following some tutorials I see that I have to configure a connection string for the DAO appender.

我的web.config中已经有一个用于数据库连接的连接字符串,所以我想知道是否可以引用它而不是设置一个新的字符串.

I already have a connection string in my web.config for DB connection so I'm wondering if I can reference that instead of setting a new one.

我通常将连接字符串更改为从开发数据库切换到生产数据库,因此共享相同的连接参数可能非常有用.

I usually change my connection string to switch from dev DB to production DB so it could be very useful to share the same connection parameters.

推荐答案

自发布以来,log4net现在支持在配置中指定连接字符串 name .来自 https://issues.apache.org/jira/browse/LOG4NET-88-

since this was posted, log4net now supports specifying the connection string name in the config. From https://issues.apache.org/jira/browse/LOG4NET-88 -

这将向AdoNetAppender类添加新的"connectionStringName"属性(和相应的ConnectionStringName)属性.这是对App.config或Web.config文件的<ConnectionStrings>部分中的连接字符串的引用.

This adds a new "connectionStringName" attribute (and corresponding ConnectionStringName) property to the AdoNetAppender class. This is a reference to a connection string within the <ConnectionStrings> section of an App.config or Web.config file.

此答案中有一个示例.

如果您未在<ConnectionStrings>部分中定义连接字符串,则可以在运行时使用此类设置连接字符串:

If you don't have the connection string defined in the <ConnectionStrings> section then you can set the connection string at runtime using this class:

在配置log4net之后,立即使用连接字符串调用SetConnectionString.

You invoke SetConnectionString with the connection string just after you've configured log4net.

但是,我要注意几件事:

However, there are a couple of things I would note:

Log4net数据库附加程序不喜欢在没有连接字符串的情况下创建,并且抛出(内部)错误-在单元测试期间,我看到给定假连接字符串的测试耗时超过10秒.

Log4net database appenders don't like being created without a connection string, and throw an (internal) error - and during unit testing, I've seen tests take more than 10 seconds when given a fake connection string.

public static class LogConfigurator
{
   public static void SetConnectionString(string connectionString)
   {
        Hierarchy logHierarchy = log4net.LogManager.GetRepository() as Hierarchy;

        if (logHierarchy == null)
        {
            throw new InvalidOperationException
               ("Can't set connection string as hierarchy is null.");
        }

        var appender = logHierarchy.GetAppenders()
                                   .OfType<AdoNetAppender>()
                                   .SingleOrDefault();

        if (appender == null)
        {
            throw new InvalidOperationException
              ("Can't locate a database appender");
        }

        appender.ConnectionString = connectionString;
        appender.ActivateOptions();
   }
}

这篇关于Log4NET AdoNetAppender连接字符串参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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