views:

53

answers:

1

This seems to be asked a lot on the internet, but so far my research hasn't produced a solution. (And, at least for now, I haven't accepted "it can't be done" as a solution.)

At its simplest description, what I'm trying to do is have a config file in a .NET class library project that is available to that class library in any application that references it. My setup can be simplified to 3 WCF services sharing some domain logic. One of the things they share is an injected dependency of a logger. In this case I have a separate library that implements the logger (this library is what gets injected) using log4net.

In this setup, each of the 3 services has its own config file and each of those config files needs to configure log4net. What I'm trying to do is fully abstract log4net into the separate library so that these 3 services (and future services/applications, of which there will be many) don't have to carry that configuration. They're all going to be configured the same way, so I'd rather it be in one place.

So far I've managed to find some code that will allow a library-localized config file of appsetttings (key/value pairs) and I can access that. But I'm looking for a more complete config file approach. Log4net has a custom configuration section that's more than key/value pairs. (Other config sections are also going to be used across multiple applications, such as the system.serviceModel section in a common library used by client applications to connect to the WCF services).

Does this make sense? Has anybody come up with a solution to this kind of thing before?

+1  A: 

You really can't have .config files with a class library in the same way as for executables! However, a class library can of course read and write xml files so you can fake it to a certain extent. This code project article describes one way of doing it, I wouldn't be surprised if there are other similar solutions, since as you say, it's a common question.

ho1
I think the problem I'll be facing in that approach (though it'll be worth a try at least) will be with the built-in config sections. What I mean is that it looks like I wouldn't be able to use system.serviceModel but would instead have to roll my own. Perhaps with enough coding I can manually create the service instances from my manually-created configuration. But it seems like that approach will fall short in 3rd party libraries such as log4net where they have their own configuration section.Unless I'm missing something.
David
@David: Haven't really used Log4Net much but there are other people with the same issue so here's a discussion about using Log4Net without config file http://osdir.com/ml/windows.dotnet.log4net.user/2004-03/msg00004.html and here's one where the solution seems to be to have the config file link to a special log4net config file http://www.mail-archive.com/[email protected]/msg03709.html, might be something for you.
ho1