Invalid Exception Formatter

Topics: Exception Handling Application Block
Aug 2, 2011 at 5:23 PM

Hi all,

I have created a custom exception formatter which extends TextExceptionFormatter. I have defined all the required constructors and overridden all abstract methods. I am using Enterprise Library 5.0.

Now when I try to use the dll in my project in FormatterType property of LoggingException Handler it gives me Invalid Exception Formatter error.

I am a beginner and this is the first time i have customized the enterprise library. Please do reply with your valuable suggestions. 

When creating the dll is there some settings i have to do?

Should the namespace of the class be same as other exception formatter class

Thanks in advance

Aug 3, 2011 at 5:54 AM

Hi,

You can try to inherit from ExceptionFormatter instead. I recommend you to take a look on the Enterprise Library source code on how the TextExceptionFormatter has been implemented so that you can have a better idea. Let me know if you have further questions.

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us

 

 

Aug 3, 2011 at 6:56 AM
Edited Aug 3, 2011 at 9:05 AM

I have already gone through the source code of TextExceptionFormatter, the problem I am getting is when I try to use my custom exception formatter as the FormatterType in my logging exception handler as I mentioned earlier.

The Xml line which gives error is:-

<exceptionHandling>
    <exceptionPolicies>
      <add name="ExceptionLogging">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="NotifyRethrow">
            <exceptionHandlers>
              <add name="LoggingException Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                logCategory="General" eventId="100" severity="Error" title="Enterprise Library Exception Handling"
               formatterType="DMP.CustomFormatter.DMPCustomExceptionFormatter, DMPCustomFormatter, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                priority="0" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>

-------My custom class

public class DMPCustomExceptionFormatter : TextExceptionFormatter
    {
        private readonly TextWriter writer;
        private int innerDepth;
        private static readonly ArrayList IgnoredProperties = new ArrayList(
            new String[] { "Source", "HelpLink", "InnerException", "StackTrace" });

        /// <summary>
        /// Initializes a new instance of the
        /// <see cref="DMPCustomExceptionFormatter"/> using the specified
        /// <see cref="TextWriter"/> and <see cref="Exception"/>
        /// objects.
        /// </summary>
        /// <param name="writer">The stream to write formatting information to.</param>
        /// <param name="exception">The exception to format.</param>
        public DMPCustomExceptionFormatter(TextWriter writer, Exception exception)
            : this(writer, exception, Guid.Empty)
        { }

        /// <summary>
        /// Initializes a new instance of the
        /// <see cref="DMPCustomExceptionFormatter"/> using the specified
        /// <see cref="TextWriter"/> and <see cref="Exception"/>
        /// objects.
        /// </summary>
        /// <param name="writer">The stream to write formatting information to.</param>
        /// <param name="exception">The exception to format.</param>
        /// <param name="handlingInstanceId">The id of the handling chain.</param>
        public DMPCustomExceptionFormatter(TextWriter writer, Exception exception, Guid handlingInstanceId)
            : base(writer, exception, handlingInstanceId)
        {
            if (writer == null) throw new ArgumentNullException("writer");

            this.writer = writer;
        }.................

The above code is an excerpt of my custom class.

My question stands -  are there some settings I have to do to use this class in LoggingExceptionHandler ? OR do I need to create the dll in any particular manner? and what about the Namespace of the class should it be something specific?

Aug 4, 2011 at 3:24 AM

Hi,

As long as your custom formatter inherits from ExceptionFormatter or from one of the exception formatters that ship with the Exception Handling Application Block, you are good. I've created my own formatter and added it throug the configuration editor and surprisingly, I got the same error. I just restarted the Visual Studio and the error goes away. Can you try it and see if it works?

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us

Aug 4, 2011 at 5:19 AM
Edited Aug 4, 2011 at 5:39 AM

Hi,

I tried restarting Visual studio after adding the custom formatter in the Web.config but still error persists. I am using Visual Studio 2010 and .Net Framework 4.0

The stack trace of exception on executing HandleException method is as follows :

at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.GetExceptionPolicy(Exception exception, String policyName) in e:\Builds\EntLib\Latest\Source\Blocks\ExceptionHandling\Src\ExceptionHandling\ExceptionPolicy.cs:line 144
   at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(Exception exceptionToHandle, String policyName) in e:\Builds\EntLib\Latest\Source\Blocks\ExceptionHandling\Src\ExceptionHandling\ExceptionPolicy.cs:line 55
   at DMP.Framework.Diagnostics.Exceptions.ExceptionLogger.PublishError(CustomException oException) in D:\POC\Exception\EnterpriseLibraryPOC\Framework.Diagnostics\Exceptions\ExceptionLogger.cs:line 74

The error is : The type 'DMP.CustomFormatter.DMPCustomExceptionFormatter,CustomFormatter, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' cannot be resolved. Please verify the spelling is correct or that the full type name is provided.

Aug 4, 2011 at 7:30 AM

This looks a real bug. I got it reproduced once in Visual Studio 2008 and it goes away after restarting VS. Moving the same application to .NET 4.0 brings the error back again. We've reached out to Microsoft regarding this and we are currently waiting for their feedback. Meanwhile, I found a similar problem from our Issue Tracker. You can upvote on this one.

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us

Aug 4, 2011 at 8:52 AM

Hi,

It seems placing a copy of your custom formatter's dll on Microsoft Enterprise Library 5.0\Bin resolves the issue. Can you try the same and see if this works?

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us

Aug 4, 2011 at 10:00 AM

Hi Noel,

Copying the dll in bin folder works. Thank you very much for your support.