Configuring a class library

Topics: Logging Application Block
Jan 13, 2009 at 2:45 PM

I have been trying out the logging app block with a simple solution setup of:

1 Windows Forms Project
1 C# class library project.

Initially I created a simple class in the class library, included the project references and added a Logger.Write statment in my class ctor. I then created an instance of my class in the WinForms project.

When the application was run it throws an exception because it is saying that it can't find the configuration source.

So, I added an app.config to my winforms project and configured it for the LAB. It then worked as expected.

My question is: If I wanted to create a class library that contained logging information must I force the host of this library (in my case the win forms app) to configure the LAB? Maybe I shouldn't be configuring the LAB in the class library?? If this is the case is there a way I can easily ignore the 'can't find configuration source' exceptions, just in case the host isn't owned/written by me?


Jan 14, 2009 at 8:05 AM
Class libraries don't own config files, Executables do.  That explains the behavior.  You could still configure a config file for a class library by making use of an IConfigurationSource. For example,

DatabaseFactory factory = new DatabaseFactory (new FileConfigurationSource('//config file location');
Database db = DabaseFactory.CreateDatabase();

Sarah B. Urmeneta
Global Technology & Solutions
Avanade, Inc.
Jan 14, 2009 at 8:15 AM
Thanks for the reply Sarah.

I understand what you're saying. Do you know if there is a way of using say the LAB in an assembly and NOT having it throw an exception if the executable that is using the dll does not want to configure the EntLib?

For example, I think that Log4Net will basically ignore any Logger statements if it hasn't been configured at all (although don't quote me on that :) )


Jan 14, 2009 at 8:44 AM
Well, if you don't want to put it in a try catch block, I think you would have to modify the source code then. 

Sarah B. Urmeneta
Global Technology & Solutions
Avanade, Inc.