Exception Handling - Get corresponding log category

Topics: Exception Handling Application Block, Logging Application Block
Oct 5, 2009 at 11:59 PM

Hi support team!

I have a screen in witch i'm trying to show the user a list of exception and the list of email who will receive this exception.

This task should by achieved by programmatically looking at the config file.

The first thing would be to know witch exceptionhandler will handle my exception (don't forget subclassing).

Then, for each categorySources contained in my exceptiontype, I should get a list of all EmailTraceListener and get all "toAdresse" field.

Can you please help me with this? 

 

Thank you in advance!

Martin

Oct 6, 2009 at 7:14 AM

Hi,

I'm not sure what this line means "The first thing would be to know witch exceptionhandler will handle my exception (don't forget subclassing). " . Is this has something to do with the scenario that you have multiple handler in one exception type? Can you please try this code snippet, this one reads the exceptionhandlingsettings, goes down to the hierarchy, from exception policy then exception type, in the exception type, it gets the logging handler from the handlers if there are multiple. It Gets the logCategory from the loggingHandler, then based on the logCategory, it will lookup from the logging settings to check if the logCategory has an associated email tracelistener. If there is it will write down the toAddresses.

            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
            fileMap.ExeConfigFilename = Assembly.GetExecutingAssembly().Location + ".config";
            Configuration entLibConfig = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

            ExceptionHandlingSettings ehabSettings = (ExceptionHandlingSettings)entLibConfig.GetSection(ExceptionHandlingSettings.SectionName);

            foreach (ExceptionPolicyData exceptionPolicy in ehabSettings.ExceptionPolicies)
            {
                Console.WriteLine(string.Format("Exception Policy: {0}", exceptionPolicy.Name));
                Console.WriteLine(Environment.NewLine + "\t");
                foreach (ExceptionTypeData exceptionType in exceptionPolicy.ExceptionTypes)
                {
                    Console.WriteLine(string.Format("Exception Type: {0}", exceptionType.TypeName));
                    Console.WriteLine(Environment.NewLine);
                    foreach (ExceptionHandlerData exceptionHandler in exceptionType.ExceptionHandlers)
                    {

                        if (Type.GetType(exceptionHandler.TypeName) == typeof(LoggingExceptionHandler))
                        {
                            LoggingExceptionHandlerData loggingHandler = (LoggingExceptionHandlerData)exceptionHandler;

                            Console.WriteLine(string.Format("Logging Handler: {0}", loggingHandler.Name));
                            Console.WriteLine(Environment.NewLine);

                            string category = loggingHandler.LogCategory;

                            LoggingSettings settings = (LoggingSettings)entLibConfig.GetSection(LoggingSettings.SectionName);

                            TraceSourceData traceData = settings.TraceSources.FirstOrDefault(v => v.Name == category);

                            foreach (TraceListenerReferenceData traceListenerReference in traceData.TraceListeners)
                            {
                                EmailTraceListenerData emailData = settings.TraceListeners.FirstOrDefault(tt => tt.Name == traceListenerReference.Name) as EmailTraceListenerData;
                                if (emailData != null)
                                {
                                    Console.WriteLine(emailData.Name);

                                    string[] toAddresses = emailData.ToAddress.Split(';');
                                    for (int i = 0; i < toAddresses.Length; i++)
                                    {
                                        Console.WriteLine(toAddresses[i]);
                                    }

                                    if (traceData.TraceListeners.Count == 0)
                                    {
                                        Console.WriteLine("\r\n");
                                    }
                                }
                            }
                        }
                    }
                }
            }

            Console.ReadLine();
Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
Oct 8, 2009 at 12:17 AM

Hi,

thanks for the fast answer. This is exactly what I wanted!!! 

Unfortunately, our config file is splitted in many files so the Enterprise Library section reside in the EnterpriseLibrary.conf. I don't know why, but the entLibConfig.GetSection(ExceptionHandlingSettings.SectionName); line return a null value just like if there was no Exception Handling section loaded.

Is there a way to load all the config file including parts located in other config files? 

Thank you again!

Martin

Oct 8, 2009 at 4:59 AM

You used the configSource attribute,right? I think it's an expected behavior.  However, I haven't found a way to get around this and you might get a faster answer in .net forums.

 

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

Oct 8, 2009 at 12:36 PM

 

 

Hi,

thanks for the fast answer! 

Here is my EnterpriseLibrary configuration part from App.config file

 

 

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <!-- Microsoft Enterprise Library configuration sections declaration. -->
        <section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common" />
        <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <section name="Alcoa.GPP.Data.NHibernate.SimpleManager.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <!--Enterprise library configuration. -->
    <enterpriseLibrary.ConfigurationSource selectedSource="EnterpriseLibraryConfig">
        <sources>
            <add name="EnterpriseLibraryConfig" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common"
                filePath="EnterpriseLibrary.config" />
        </sources>
    </enterpriseLibrary.ConfigurationSource>

.......

 

and here is a part of my EnterpriseLibrary.config file

 

 

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null" />
    <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null" />
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null" />
    <section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null" />
  </configSections>
  <loggingConfiguration name="Logging Application Block" tracingEnabled="false"
    defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
    <listeners>

......

 

It works perfectly inside the application, at run time. But when I try to access the EntLib part with this line of code

 

ExceptionHandlingSettings ehabSettings = (ExceptionHandlingSettings)entLibConfig.GetSection(ExceptionHandlingSettings.SectionName);

provided in your first reply, I get variable ehabSettings null.

Thank you again for your good support.

Martin

 

 

Oct 9, 2009 at 12:36 PM

Hi,

Just to let you know I found a way to read my EntLib config, as described in the above post.

I only replaced 

Configuration entLibConfig = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

by

IConfigurationSource entLibConfig = ConfigurationSourceFactory.Create();

 

ConfigurationSourceFactory takes care of loading the application config file and attaching the enterprise library section from other config file, if needed.

Thanks again!

Martin