configuration of customHandlers

Topics: Exception Handling Application Block
May 15, 2007 at 10:13 AM
Edited May 15, 2007 at 10:17 AM
Hello,

today I tried to extend the exception Handling Application Blog with an ExceptionHandler of my own. I use the local dlls and alreade switched the registry according to set the default on the correct directory. This is the implementation of the ExceptionHandler:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;
using log4net;
 
namespace Brenk.ExceptionLogHandler
{
    [ConfigurationElementType(typeof(CustomHandlerData))]
    public class ExceptionLogHandler: IExceptionHandler 
    {
        /// <summary>
        /// Logger (log4net) für Exception-Logging
        /// </summary>
        private ILog log ;
 
        /// <summary>
        /// Konstruktor mit Übergabe des Methodennamens der
        /// Methode, in der die Exception aufgetreten ist
        /// </summary>
        /// <param name="MethodenName">Name der aufrufenden Methode</param>
        public ExceptionLogHandler ()
        {
            log = LogManager.GetLogger("ExceptionHandler");
        }
       
        #region IExceptionHandler Member
 
        /// <summary>
        /// Loggen der Exception
        /// </summary>
        /// <param name="exception">Exception</param>
        /// <param name="handlingInstanceId"></param>
        /// <returns>Die gleiche Exception, die reingekommen ist</returns>
        public Exception HandleException (Exception exception, Guid handlingInstanceId)
        {
            string nachricht = "Exception-Message: " + exception.Message + "\r\n" +
                             handlingInstanceId.ToString();
            log.Error(nachricht, exception);
            return exception;            
        }
 
        #endregion
    }

This is a class in an Library-Project. This lib, the application and the EnterpriseLib are in separate directories and the above project links the local dlls of log4net and of the EnterpriseLib.

Trying to add this class as ExceptionHandler using the configurationTool leads to the known error:
"there were no types found in the assembly ... that implement or inherit from the base type ....IExceptionHandler"

As I found some posts over here, I tried and copied the ExceptionHandler.dll in the same directory as the EnterpriseLibs dlls. Didn't work. All dlls are linked to the applications Main project. If I add the ExceptionHandling-Tags manually to the app.config the ConfigurationTool won't show the ExceptionHandling-Configuration next time I open the file.
Manual configuration:

<exceptionHandling>
    <exceptionPolicies>
      <add name="Exception Policy">
        <exceptionTypes>
          <add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="NotifyRethrow" name="Exception">
            <exceptionHandlers>
              <add type="Brenk.ExceptionLogHandler.ExceptionLogHandler, Brenk.ExceptionLogHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                name="ExceptionLogHandler" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>

throwing an exception using the manually configured ExceptionHandler gives an Exception:
"No constructor for ExceptionLogHandler" what is wrong? I don't have any more ideas. Please help!

thanks

Claudia
May 15, 2007 at 11:59 AM
I found the solution by accident. Everything works fine since I installed the guidance Automation Tookit and Extensions. That's all. Perhaps it would be a good idea to add this to the documentation.....
May 15, 2007 at 6:17 PM
You don't need the GAT/GAX to extend Enterprise Library.

If you want to use the Application Block Software Factory to help build providers or new application blocks, you will need GAT, GAX, and .NET 3.0.

The key for the first error you mentioned is that you probably needed the:

ConfigurationElementType(typeof(CustomHandlerData))

attribute on the class.

Off the top of my head, the second error occurred because you probably didn't have a constructor that took a NameValueCollection to pass in customer attributes as specified by the CustomHandlerData in the configuration element.

I would have to see the class to be for sure on all these errors, but I know at one point I have received all of them when learning this stuff :)

But again, you don't need to have the GAT/GAX installed unless you are using the software factories / recipes put out by the P&P Team. The Application Block Software Factory happens to be one of them.

Regards,

Dave

__________________________________

David Hayden
Microsoft MVP C#