Edit: since this was published, now log4net supports specifying the connection string name in the configuration. From https://issues.apache.org/jira/browse/LOG4NET-88 -
This adds the new connectionStringName property (and the corresponding ConnectionStringName property) to the AdoNetAppender class. This is a link to the connection string in the <ConnectionStrings> section of the App.config or Web.config file.
Here is an example in this answer .
If you do not have the connection string defined in the <ConnectionStrings> section, you can set the connection string at runtime using this class:
You call SetConnectionString using the connection string immediately after setting up log4net.
However, there are a few things I would like to point out:
Log4net database subscribers do not like to create without a connection string and cause an (internal) error - and during unit testing, I saw that the tests take more than 10 seconds when they are assigned a false connection string.
public static class LogConfigurator { public static void SetConnectionString(string connectionString) { Hierarchy logHierarchy = log4net.LogManager.GetRepository() as Hierarchy; if (logHierarchy == null) { throw new InvalidOperationException ("Can't set connection string as hierarchy is null."); } var appender = logHierarchy.GetAppenders() .OfType<AdoNetAppender>() .SingleOrDefault(); if (appender == null) { throw new InvalidOperationException ("Can't locate a database appender"); } appender.ConnectionString = connectionString; appender.ActivateOptions(); } }
source share