0

I am configuring the connection string for an AdoNetAppender dynamically.

The issue I am running into is, that log4net.Config.XmlConfigurator.Configure(); is trying to establish a connection to the database, but the connection string doesn't get set until the following line (ConfigureLog4Net(logConnString);)

Global.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
            }
        }
    }
}
blgrnboy
  • 4,877
  • 10
  • 43
  • 94
  • Well, yeah -- `XmlConfigurator.Configure` configures everything, including any active appenders. You're trying to have your cake and eat it too. You could read the XML explicitly and manipulate it before passing it to `XmlHierarchyConfigurator`, or you could create a wrapper class for the `AdoNetAppender` that delays initialization/validation of the connection string, or springboard off a `ForwardingAppender`, or explicitly configure the appender to be `OFF` before turning it on in code (unsure if that works), or even proxy the ADO provider. None of that feels very nice, though. – Jeroen Mostert May 08 '17 at 21:26
  • [This](http://stackoverflow.com/questions/13509988/log4net-adonetappender-connection-string-reference) could be helpful – FortyTwo May 08 '17 at 21:36
  • @JeroenMostert do you have an example of a wrapper of AdoNetAppender? – blgrnboy May 08 '17 at 21:42
  • No, I'm just tossing out ideas. It may be as simple as subclassing it and overriding `ActivateOptions`, though (say, check if the connection string is empty and return immediately if it is, otherwise, invoke `base.ActivateOptions()`). – Jeroen Mostert May 08 '17 at 21:44
  • As another option, as it appears that your connection string is stored in the `ConnectionStrings` section, you can define the [`ConnectionStringName`](https://logging.apache.org/log4net/release/sdk/html/P_log4net_Appender_AdoNetAppender_ConnectionStringName.htm) you want log4net to use. – stuartd May 09 '17 at 11:13

1 Answers1

0

You can configure log4net to get the value from the ConnectionStrings instead of adding a connection string itself to you log4net configuration. Instead of connectionString add:

<connectionStringName value="LoggingConnection" />
Peter
  • 27,590
  • 8
  • 64
  • 84