26

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.

Stuart Childs
  • 3,275
  • 1
  • 17
  • 11
Gai
  • 315
  • 1
  • 5
  • 8

3 Answers3

16

It appears you already have found a solution that works for you. However, I've found a different solution which I consider to be "more ideal", so I'll post it here for whoever might find this question in the future.

log4net has a concept called repositories which can be configured separately. It's not very popular nor very well documented, but here's some documentation I found:

http://logging.apache.org/log4net/release/manual/repositories.html

Anyway, all you need to do is add RepositoryAttribute on your plugin assembly or assemblies with a repository name unique to that plugin and log4net will keep it separate from everything else.

Tinister
  • 10,249
  • 6
  • 32
  • 35
4

Yet another way is to have multiple log4net configuration files in multiple places, load them all up in an XDocument to effectively compose a single one, and then call XmlConfigurator.Configure(). The way I did this was to create each file following the XSD for the configuration, load all the children of each file's log4net root node into separate XElement instances and merge them into a new XDocument with a root log4net node. I'll update this answer with the code (it's not in front of me at the moment) if anyone's interested further.

Finding the files is another matter: use some sort of convention to scan for files (e.g. *.dll.log4net.config), embed them in the assemblies, or something.

An implementation of such code can be found on:

www.kopf.com.br/kaplof/using-multiple-configuration-files-with-log4net

Bruno Marotta
  • 158
  • 1
  • 8
Kit
  • 15,260
  • 3
  • 47
  • 87
  • How would you do this? Sounds like a good solution. It seems non-trivial since you need to keep only a single "log4net" tag and merge the contents of that tag. – Wayne Dec 08 '10 at 01:21
  • I updated my answer with a bit more detail. It turns out not to take too much code, and was well worth it for having different logging settings per environment without resorting to additional build steps. – Kit Dec 12 '10 at 02:41
2

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.

Stuart Childs
  • 3,275
  • 1
  • 17
  • 11