Can logging configuration be extended at runtime?

Topics: Logging Application Block
May 19, 2010 at 11:20 PM

I want to use the Logging application block from Enterprise Library 5.0 in my application. However, I don't want the web.config file to contain any configuration details for the default trace listener. I want to hardcode the default trace listener in the application code so that it cannot be altered by the user via the config file. But still want the admin to be able to configuration additional trace listeners in the web.config. So basically I want the logging block to read any configuration data from the web.config file but during the application initialization, I want to add my own listener in addition to the configured ones.

Is this possible? I tried the fluent configuration API and configuration builder to achieve this, but when I call configBuilder.UpdateConfigurationWithReplace() method, it commits my additions to the web.config file. Is it possible to later the configuration in memory without persisting it?

Thanks.

May 20, 2010 at 3:26 AM

A way I could think of to do this would be to create a separate file that will contain your additions during runtime and make use of the ParentConfigurationSource that is new in EntLib 5.0.  Here's the steps:

1. Create a separate file, say logging. config.  This is where you will put your own listeners during runtime.

2. Go to your application's configuration file and add the Configuration Sources if it doesn't exist yet. (Blocks -> Add Configuration Settings).

3. Add the System Configuration Source in the Source section.

4. Add a File-based Configuration Source in the Sources section.  Specify the full path of the logging.config in the FilePath property.

5. Under the Configuration Settings, locate the ParentSource property, set it to your file-based configuration source.  Set the Selected Source to the System Configuration Source.  You can also refer to the documentation (Developing Applications with Enterprise Library -> Configuring Enterprise Library -> Advanced Configuration Scenarios -> Applying a Common Structure for Applications) for a reference on how this kind of configuration works but basically, it merges the two (the Parent and the Selected).

6. Now, when using the Fluent configuration Api, you would create an instance of FileConfigurationSource referring to your logging.config file

IConfigurationSource configSource = new FileConfigurationSource("pathtoLogging.config");
builder.UpdateConfigurationWithReplace(configSource);

  Let me know if anything is unclear.  

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@Avanade.com