How to add log4net appender in runtime?

You should also add the appender to a logger. Take a look here Adding Appenders programmatically

If the logger you are using is, for example ILog logger do:

((log4net.Repository.Hierarchy.Logger)logger.Logger).AddAppender(appender)


var patternLayout = new log4net.Layout.PatternLayout
{
    ConversionPattern = "%date %level %message%newline"
};
patternLayout.ActivateOptions();

var rollingFileAppender = new log4net.Appender.RollingFileAppender
{
    File = "MyApp.log",
    Layout = patternLayout
};
rollingFileAppender.ActivateOptions();

var hierarchy = (log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetRepository();
hierarchy.Root.AddAppender(rollingFileAppender);
hierarchy.Root.Level = log4net.Core.Level.All; // Default is Debug

log4net.Config.BasicConfigurator.Configure(hierarchy);

Tags:

C#

.Net

Log4Net