Chad,

Appenders are attached to Loggers. Currently the only way to get a list of
all the appenders is to iterate over all the loggers. Remember that an
appender instance can be attached to several loggers. We can currently get
the list of appenders using the following code:

System.Collections.Hashtable appendersMap = new
System.Collections.Hashtable();
foreach(log4net.ILog log in log4net.LogManager.GetCurrentLoggers())
{
  foreach(log4net.Appender.IAppender appender in
((log4net.Repository.Hierarchy.Logger)log.Logger).Appenders)
  {
    appendersMap[appender.Name] = appender;
  }
}

This code is not great because of the casting.
There is actually a problem with this code because it assumes that there
won't be two different appenders with the same name. This is not enforced.
To be totally correct we would either need to use a multi map or key the map
on the appender instance.

In future we may look at enforcing unique appender names and maintaining a
definitive list of appenders on the repository object.

Nicko


> -----Original Message-----
> From: Chad Myers [mailto:[EMAIL PROTECTED] 
> Sent: 10 March 2004 15:38
> To: Log4NET User
> Subject: RE: config-free log4net?
> 
> The API does not allow (at least I couldn't find) certain key 
> features necessary for full run-time code-only configuration, 
> such as getting a list of all appenders specified, the 
> mapping of appenders to logs, etc.
> 
> What I did was write a config file to the filesystem and use
> ConfigureAndWatch() on DOMConfigurator. Then, as the user 
> changes things, it will re-write this file and log4net will 
> pick up the changes.
> 
> I have to do some locking and such to ensure that two users 
> don't collide while making changes, but other than that it 
> works reasonably well.
> 
> Are there any plans for updating the API to allow full 
> run-time code-only configuration?
> 
> Sincerely,
> Chad
> 
> -----Original Message-----
> From: Nicko Cadell [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, March 10, 2004 9:35 AM
> To: 'Log4NET User'
> Subject: RE: config-free log4net?
> 
> Chad,
> 
> > I found that there is no real good way to configure log4net at 
> > runtime. There is not a full API set for doing this and 
> what methods 
> > appear to provide some of the functionality don't seem to work 
> > (they're used internally by log4net and don't seem to be 
> intended to 
> > be used externally).
> 
> The API is there for defining configuration at runtime, but 
> it is not strongly typed. This allows new appenders, layouts, 
> etc to be written without requiring changes to the log4net 
> interfaces. This does mean that configuring log4net 
> programmatically isn't as easy as it could be.
> 
> One way to configure log4net is to embed an xml config file 
> as an embedded resource in your assembly and use that to 
> configure logging.
> 
> Stream configStream =
> Assembly.GetExecutingAssembly().GetManifestResourceStream("Log
> gingConfig
> .xml
> ");
> log4net.Config.DOMConfigurator.Configure(configStream);
> configStream.Close();
> 
> This gives you the flexibility of the config file but without 
> the deployment issues.
> 
> Also the DOMConfigurator merges its changes into the 
> repository, therefore it would be possible to override this 
> embedded configuration by using the
> DOMConfigurator.ConfigureAndWatch(FileInfo) to monitor for an 
> external config file. This file does not have to exist, but 
> if it does then it will be loaded and merged into the repository.
> 
> Nicko
> 
> 
> 

Reply via email to