Programatically enabling/disabling category filters

Jul 1, 2009 at 8:31 PM

Hi we have a category filter in our configuration file to turn on/off tracing.  In production, we want turning tracing on and off to be done via a button - not manually editing the configuration file. 

This is the XML in the config file

<categoryFilters><add name = "PostSharpTrace"/></categoryFilters> 

This is the C# code I have thus far to open the configuration file, I thought the logging section was read only so we could do this?


CategoryFilter cfCategoryFilter = Logger. GetFilter<CategoryFilter>();

ExeConfigurationFileMap filemap = new ExceConfigurationFileMap()

filemap.ExeConfigFilename = Application.StartupPath +"\\" + EnterpriseLibraryClient.config":

Configuration entlibConfig = ConfigurationManager. OpenMappedExeConfiguration(filemap, ConfigurationuserLevel.None);

// Modify the value in the configuration file

if (TurnTracingOn == true)

// how do I Add the value to the category?


// how do I remove the value to the category?


Thanks Jess


Jul 2, 2009 at 6:23 AM

LoggingSettings loggingSettings = (LoggingSettings)entlibconfig.GetSource("loggingConfiguration");

var filter = loggingSettings.LogFilters.Get("Category Filter");

Adding value to the category:

 ((CategoryFilterData)filter).CategoryFilters.Add(new CategoryFilterEntry("GeneralCategory"));

Remove a value from the category:




Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.


Jul 2, 2009 at 6:03 PM

Thanks, but I get a compile error on this line:  LoggingSettings loggingSettings = (LoggingSettings)entlibconfig.GetSource("loggingConfiguration");

Error: "System.configuration.configuration does not contain a definition for 'GetSource' and no extension method 'GetSource' accepting a first argument of type Syste.Configuratin.Configuration coudl be found"

Does this compile for you?

I tried changing to GetSection but that gives an error as well:

Error Cannot convert type 'System.Configuration.ConfigurationSection to 'Microsoft.Practices.EnterpriseLIbrary.Logging.Configuration.LoggingSettings




Jul 2, 2009 at 6:51 PM

Actually I think the syntax is this:

LoggingSettings loggingSettings = (LoggingSettings)configuration.Sections.Get(LoggingSettings.SectionName);

So all seams to be working, but can you tell me how to force the application reconsult the configuration file settings immediatly after I call





Jul 3, 2009 at 4:08 AM

Yup, that was GetSection, not GetSource, my fault.  Not sure why that change didn't worked for you though.  Anyway, here's  a thread relating to your second question -


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.