I have log4net configured to write to a database. My log config looks something like this:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<root>
<appender-ref ref="SqlAppenderAll" />
<level value="DEBUG" />
</root>
<appender name="SqlAppenderAll" type="log4net.Appender.ADONetAppender">
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionStringName value="MyConnectionString" />
...
</appender>
</log4net>
The problem is, MyConnectionString is not available immediately. A number of configuration items for the service are stored in a config database, and retrieved when the service starts. This allows the connection strings to be configured once for the entire system, rather than at each service individually.
So when the service starts, it does something like this:
var myConnectionString = ConfigProvider.GetConnectionString("MyConnectionString");
AddConnectionString(myConnectionString, "web.config");
This causes log4net to ignore the sql appender because the connection string isn't available when it initializes.
log4net:ERROR [AdoNetAppender] ErrorCode: GenericFailure. Could not open database connection []. Connection string context [Unable to determine connection string context.].
log4net.Core.LogException: Unable to find [MyConnectionString] ConfigurationManager.ConnectionStrings item
at log4net.Appender.AdoNetAppender.ResolveConnectionString(String& connectionStringContext)
at log4net.Appender.AdoNetAppender.InitializeDatabaseConnection()
Any subsequent calls to log.Info/Debug/etc never get written.
Is there a way to make log4net retry the connection after adding the connection string to the web.config. Alternatively, is there a way to postpone the log4net init process?