Setting a log4net AdoNetAppender Connection String at Runtime

I recently implemented log4net in a project to log to a database using the log4net AdoNetAppender class. But I had a need to be able to specify the connection string at runtime. Here's how I was able to do it. Hopefully this will help out someone who has the same need. :)

protected void SetLog4NetAppenderConnectionString(string connectionString)
    log4net.Repository.Hierarchy.Hierarchy hierarchy =
            as log4net.Repository.Hierarchy.Hierarchy;
    if (hierarchy != null)
        log4net.Appender.AdoNetAppender appender
            = (log4net.Appender.AdoNetAppender)hierarchy.GetAppenders()
                .Where(x => x.GetType() == 
        if (appender != null)
            appender.ConnectionString = connectionString;


Popular Posts

How To Mock Out Child Components In Unit Tests of Angular 2 Code

A Generic Method Using HttpClient to Make a Synchronous Get Request

The Cause and Solution for the "System.Runtime.Serialization.InvalidDataContractException: Type 'System.Threading.Tasks.Task`1[YourTypeHere]' cannot be serialized." Exception

A Red Herring When Using Moq to Mock Methods With Optional Parameters

Unit Testing with a Mock Entity Framework DbContext and Fake DbSets