Best way to test for existence of formatters, tracelisteners, etc for a given category.

Topics: Data Access Application Block, Exception Handling Application Block, General discussion, Logging Application Block
May 21, 2010 at 11:26 AM

Hello all,

The HandleExeption method takes a Category parm and handles everything for you. If you wanted to check in your code what listeners, exceptions, formatters, etc were attached to that category, what is the best way to retrieve that information? Thanks in advance for any ideas and/or suggestions, especially code examples :-).

May 24, 2010 at 2:25 AM

Here's a sample code:

public void TraverseExceptionPolicy()
            //Create instance of IConfigurationSource from the default config file
            IConfigurationSource configSource = ConfigurationSourceFactory.Create();

            //Get ExceptionHandlingSettings
            ExceptionHandlingSettings exceptionHandlingsettings =(ExceptionHandlingSettings)configSource.GetSection(ExceptionHandlingSettings.SectionName);
            //Get the Exception policy with the name "Exception Policy"
            ExceptionPolicyData exceptionPolicy = exceptionHandlingsettings.ExceptionPolicies.FirstOrDefault(e => e.Name == "Exception Policy");
            foreach(ExceptionTypeData exceptionData in exceptionPolicy.ExceptionTypes)
                foreach (ExceptionHandlerData handlerData in exceptionData.ExceptionHandlers)
                    LoggingExceptionHandlerData loggingHandler = (LoggingExceptionHandlerData)handlerData;
                    //check if the exception handler is a logging handler
                    if (loggingHandler != null)
                        DisplayLoggingCategory(configSource, loggingHandler.LogCategory);

public void DisplayLoggingCategory(IConfigurationSource configSource, string logCategory)
            LoggingSettings loggingSettings = (LoggingSettings)configSource.GetSection(LoggingSettings.SectionName);
            //Get the logging category
            TraceSourceData traceSource = loggingSettings.TraceSources.FirstOrDefault(c => c.Name == logCategory);
            foreach (var traceListenerRef in traceSource.TraceListeners)
                TraceListenerData traceListener =
                    loggingSettings.TraceListeners.First(t => t.Name == traceListenerRef.Name);

The FormatterType property is not available to the TraceListenerData object because it's not common to all of its subclasses such as the XmlTraceListenerData.

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.