disabling tracelistener at run-time (ent lib 3.1)

Topics: Exception Handling Application Block, Logging Application Block
Jul 27, 2009 at 12:47 PM

Hi,

I'm looking for a way to disable a trace listener at run-time, if possible without changing app.config.

Something like:

LoggingSettings settings = LoggingSettings.GetLoggingSettings(new SystemConfigurationSource());
settings.TraceListeners.Remove("Email Trace Listener");

[throws an exception since settings are read-only]

I want to turn exception emails on/off programmatically.

Thanks,

Ralf

Jul 28, 2009 at 7:27 AM

Hi,

Is your Email trace listener on a different category or it is in the same categories as the other trace listener? What is your exact scenario for enabling and disabling the tracelistner?

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com 

Jul 28, 2009 at 8:39 AM

Hi,

The email trace listener is added to a single category source "General". This category source has 2 more trace listeners and is used for logging exceptions. 5 trace listeners are defined for the application, and about 10 category sources.

The scenario is: I want to disable exception emails based on user credentials and/or user input in an options dialog.

Thanks,

Ralf

 

Jul 28, 2009 at 12:39 PM

Hi,

I just realized that changing the config at runtime, the application wont detect the changes untill the application restarts. We'll investigate further on this if we can find a work around.

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com 

Jul 29, 2009 at 2:35 AM
Edited Jul 29, 2009 at 2:38 AM

Hi Ralf, I'm basing the information below in the 4.1 version but it might be worth to try if it would work in the 3.1 version.  You would have to modify the configuration file one way or another in order to achieve what you want.  If the reason for not wanting to modify it during runtime is because of the LoggingSettings being a read-only object, there is a way around it.  Here's the sample code:

public static void DisableTraceListener(string categoryToDisable)
{
ExeConfigurationFileMap filemap = new ExeConfigurationFileMap();
filemap.ExeConfigFilename = @"path_to_the _config";
Configuration entlibConfig = ConfigurationManager.OpenMappedExeConfiguration(filemap, ConfigurationUserLevel.None);
IConfigurationSource configSource = new SystemConfigurationSource();
LoggingSettings loggingSettings = (LoggingSettings)entlibConfig.GetSection("loggingConfiguration");
var filter = loggingSettings.LogFilters.Get("Category Filter");
if (filter != null)
{
      CategoryFilterData categoryFilter = (CategoryFilterData)filter;
      categoryFilter.CategoryFilters.Clear();
      categoryFilter.CategoryFilterMode = CategoryFilterMode.AllowAllExceptDenied;
      ((CategoryFilterData)filter).CategoryFilters.Add(new CategoryFilterEntry(categoryToDisable));
}
entlibConfig.Save();
}

Note that this modifies directly the logging section of the app.config.  You can also choose to have an external configuration file for the logging section.  Refer to this link - http://msdn.microsoft.com/en-us/library/cc511916.aspx.  As mentioned there, only changes to logging section can be detected during runtime both for win app and web app without application restart.

 

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

Jul 29, 2009 at 7:10 AM

Hi Sarah,

Thanks a lot for explanation and code snippet!

Ralf