log4net Configure() 尝试在设置 connString 之前建立 SQL 连接



我正在动态配置AdoNetAppender的连接字符串。

我遇到的问题是,log4net.Config.XmlConfigurator.Configure();正在尝试建立与数据库的连接,但直到以下行 (ConfigureLog4Net(logConnString);( 才设置连接字符串

全球.asax.cs

protected void Application_Start()
{
    // Other configuration
    // Configure logging
    var logConnString = ConfigurationManager.ConnectionStrings["LoggingConnection"].ToString();
    if (logConnString != null)
    {
        log4net.Config.XmlConfigurator.Configure();
        ConfigureLog4Net(logConnString);
    }
}
private void ConfigureLog4Net(string logConnString)
{
    Hierarchy hierarchy = LogManager.GetRepository() as Hierarchy;
    if (hierarchy != null && hierarchy.Configured)
    {
        foreach (IAppender appender in hierarchy.GetAppenders())
        {
            if (appender is AdoNetAppender)
            {
                var adoNetAppender = (AdoNetAppender)appender;
                adoNetAppender.ConnectionString = logConnString;
                adoNetAppender.ActivateOptions(); //Refresh AdoNetAppenders Settings
            }
        }
    }
}
您可以配置 log4net

以从 ConnectionString 中获取值,而不是将连接字符串本身添加到 log4net 配置中。代替连接字符串添加:

<connectionStringName value="LoggingConnection" />

相关内容

  • 没有找到相关文章

最新更新