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

前端 未结 5 1858
花落未央
花落未央 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

    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 = ...   }
        }
    }
    
    0 讨论(0)
  • 2020-12-05 04:27

    It is possible to use a DB connection string specified in web.config without creating a new class, though you would need to use log4net build that hasn't been released yet. It can be downloaded from SVN repository http://svn.apache.org/viewvc/logging/log4net/trunk/

    Your config will look as follows:

    <connectionStrings>
        <add name="connStr" connectionString="Data Source=localhost; ..." />
    </connectionStrings>
    
    <log4net>
        <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
        <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        <connectionStringName value="connStr" />
          ...
    </log4net>
    

    Please note that connectionType still needs to be specified.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-05 04:29

    As of 2017 (log4net 2.0.8.0), the following works:

    public class MyAdoNetAppender : AdoNetAppender
    {
        public MyAdoNetAppender()
        {
            ConnectionString = ...
        }
    }
    
    
    <appender name="AdoNetAppender" type="MyApp.MyAdoNetAppender">
    

    (Very similar to @Michael Petrotta's answer)

    0 讨论(0)
  • 2020-12-05 04:50

    fyi this will be implemented in 1.2.11 according to this. however I have no idea when they are going to release it.

    0 讨论(0)
提交回复
热议问题