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
}
}
}
}