收录日期:2019/04/18 21:12:30 时间:2010-06-22 20:31:10 标签:.net,app-config,class-library

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?

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.