Dynamically changing loging levels using Logging App Block

Topics: Logging Application Block
Mar 6, 2007 at 12:29 AM
Is it possible to dynamically change the logging level of a logger category at runtime? For example, if my config file looks like this:
<add switchValue="Error" name="General">
<add name="RollingFile TraceListener" />

Can I progrmatically change the Level from "Error" to "Off", "Verbose", etc.

I was hoping to do something like this, but it doesn't work (The configuration is read only.):

LoggingSettings settings = LoggingSettings.GetLoggingSettings(new SystemConfigurationSource());
NamedElementCollection<TraceSourceData> sources = settings.TraceSources;
foreach (TraceSourceData source in sources)
source.DefaultLevel = SourceLevels.Information;

Mar 6, 2007 at 4:00 PM
You can't do it via the LoggingSettings Class because as you noticed it is read only.

To the best of my knowledge there is no easy way to do it unless you want to

1) Programmatically manipulate the configuration information as mentioned here without using an IConfigurationSource:

Enterprise Library 2.0 Logging Application Block Part III - Programmatically Using Logging Application Block With No Configuration File

2) Modify the IConfigurationSource ( Web.config, app.config, or external config file ) during runtime where the Logging Application Block gets its LoggingSettings. Enterprise Library uses a File Watcher Class to look for changes in the underlying configuration source and reload the settings upon modification. You could of course build your own IConfigurationSource, too, as mentioned in the quickstarts about creating your own SqlConfigurationSource.

Those are the only 2 ways that I know of to make configuration changes like you want on the fly without modifying the EntLib source code.




David Hayden
Microsoft MVP C#
Mar 6, 2007 at 11:02 PM
Edited Mar 6, 2007 at 11:20 PM
Thanks. I'm surprised there is no way to do this. In my experience, editing the web.config file of a web service application causes all session information to be lost. That doesn't make me want to use this framework for web services!
Mar 6, 2007 at 11:25 PM
Edited Mar 6, 2007 at 11:26 PM
Don't use the web.config to store your logging configuration information. Use a separate configuration file.

Again, Enterprise Library can get its configuration information from an external configuration file or any source that implements IConfigurationSource.

More information about configuration in a thread I answered today:


I wouldn't say you wouldn't use this framework for web services since it is used successfully by many enterprise applications via the Web Service Software Factory:

Web Service Software Factory




David Hayden
Microsoft MVP C#
Mar 7, 2007 at 12:31 AM
Just to add to this, the Logging Application Block is able to detect configuration changes and reload the configuration without restarting the application (although note that the other blocks do not support this capability). This works for web apps as well as Windows apps, but as Dave points out changing the web.config file will automatically trigger an AppDomain recycle for ASP.NET apps. This isn't necessarily a bad thing for a web app but can be bad if you rely on in-process session state (which seems to be your situation). Dave's suggestion on using an external file for the Logging configuration is the best workaround here, since ASP.NET doesn't know about the file and won't refresh the AppDomain if you change it.

Mar 7, 2007 at 4:46 PM
Great! I will look into using an external configuration file for logging.
Thank you both for your help!

Mar 16, 2007 at 10:55 AM
Hi, I wanted to ask you a question regarding : "the Logging Application Block is able to detect configuration changes and reload the configuration without restarting the application "

We have an app that receives many events per seconds, so we log them at the beginning, just to make sure all events arrive fine, but then we turn it off for performance and disk space reasons. The problem is, if we turn it off by the config file, there is still all the calls (and memory allocations) left. What we did was to call a ShouldLog at the beginning of the app, so we could spare all the reources. But then, we loose the ability to turn it off or on at runtime.

What I want to know is if there is an event (it looks like it, because there are delegates present) to subscribe so that I could be notified of the change of that logging key.

Any help is very much appreciated.
Mar 16, 2007 at 5:14 PM
I have only done it as a demo during a presentation and never in a production environment, but here is a copy and paste showing how I demo the functionality for detecting changes in the IConfigurationSource of your choice:

        private void Form1_Load(object sender, EventArgs e)
            IConfigurationSource source = new FileConfigurationSource("EntLib.config");
            source.AddSectionChangeHandler("loggingConfiguration", ChangeHappens);
        private void ChangeHappens(object sender, ConfigurationChangedEventArgs e)
            MessageBox.Show(string.Format("Change Happens to Section: {0}", e.SectionName));

In this case I am watching a FileConfigurationSource called EntLib.config in the application's root directory. I specify I am only concerned with the loggingConfiguration Section. You could add more. Maybe someone can offer some real world use of this functionality.




David Hayden
Microsoft MVP C#
Nov 5, 2008 at 10:35 AM

From: kk16882


I have diffrent application environments: Development, Test, Staging, Production, etc
Currently, I am logging using Database Trace Listner programatically.
Now, I need to load environment specific details (e.g connection string for Development, Test, Staging, Production etc) programatically.
How/Where can i specify environment specific details in my web.config or anywhere else?

Anyone have sample example for above scanerio?