Can you pull the connectionString for a log4net AdoNetAppender from elsewhere in a web.config file?

前端 未结 5 1871
花落未央
花落未央 2020-12-05 03:53

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

5条回答
  •  攒了一身酷
    2020-12-05 04:27

    the answers above all do not work. i got another solution for this, i tried and it worked:

    private static void ConfigureLog4Net()
    {
        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 = ConfigurationManager.AppSettings["YOURCONNECTIONSTRINGKEY"].ToString();
                   adoNetAppender.ActivateOptions(); //Refresh AdoNetAppenders Settings
               }
            }
        }
    }
    

    How i can use the connectionString of the current website for log4Net instead of configuring

提交回复
热议问题