You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by Mauro Rallo <ra...@prosa.com> on 2005/08/23 17:40:57 UTC

Configure Log4net from code at RunTime

Hi, I need to add a child to the root category of logging at runtime.
I configure log4net at the startup of my application with the app.config file.
After I need to add some category with differents appenders.

I tried with this code, but it doesn't work :

Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
Logger rootLogger = hierarchy.Root;
			
Logger coreLogger = hierarchy.LoggerFactory.CreateLogger("abc");
coreLogger.Parent=rootLogger;
RollingFileAppender roller = new RollingFileAppender();
PatternLayout patternLayout = new PatternLayout();
patternLayout.ConversionPattern = LogPattern;
patternLayout.ActivateOptions();
roller.Layout = patternLayout;
roller.AppendToFile = true;
roller.RollingStyle = RollingFileAppender.RollingMode.Size;
roller.MaxSizeRollBackups = 4;
roller.MaximumFileSize = "100KB";
roller.StaticLogFileName = true;
roller.File = "foobar.txt";
roller.ActivateOptions();
coreLogger.RemoveAllAppenders();
coreLogger.AddAppender(roller);

log4net.ILog log3 =LogManager.GetLogger("abc");
log3.Debug("This message has to go to the foobar.txt File");

In the documentation of the Hierarchy class is write that "the hierarchy is such
that children link to their parent but parents do not have any references to
their children." 
But how I can say to the System that I added a new child?

It's possible to make it?
Someone can help me?

Thanks a lot

Mauro



Re: Configure Log4net from code at RunTime

Posted by Mauro Rallo <ra...@prosa.com>.
Ok, I found my mistake. I've to take the new Logger to configure from the
Hierarchy class and not from the LoggerFactory.
So the correct code is :

Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
Logger rootLogger = hierarchy.Root;
Logger coreLogger = hierarchy.GetLogger("abc") as Logger;
RollingFileAppender roller = new RollingFileAppender();
// .... create and configure appender ...///
coreLogger.AddAppender(roller);

I hope that can be useful to other people.

Mauro