log4netlog4net-configuration

log4net - configure using multiple configuration files


I have an application consisting of a host and pluggable modules (plugins).

I want to be able to configure log4net for the host and for each of the other modules. Each of them should have its own configuration file and each will log to a different file.

Only the host has an App.config file. The plugins have their own config file containing the log4net config sections.

Calling XmlConfigurator.Configure from one of the plugins overrides the host's app.config log4net definitions.

Is there an easy way to append configurations instead of overriding them?

Thanks, Gai.


Solution

  • I just ran into this problem myself. While it's not exactly what I'd call "ideal," I think the best solution currently is to use the ConfigureAndWatch method of the XmlConfigurator class. Basically, you have your host assembly setup the log4net configuration using a specific configuration file. When your plugins load, have them write their configuration elements into the log4net configuration section in that same configuration file. Since log4net has been told to watch that file for changes with ConfigureAndWatch, when the file has that new data added, log4net will reload the configuration which will now include the configuration elements from the plugin(s).

    This is a bit hackish, but it seems to work. The next step of course will be moving this into my logging framework so that access to the configuration file is federated.

    Do note that there is a short delay between the plugin's configuration being written/saved and the updated configuration being reloaded and applied to the logger repository.