Adding a New Exception Formatter

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

To add a new exception formatter, you must create a new exception class that derives from the ExceptionFormatter class. Your new class can derive directly from ExceptionFormatter or from one of the exception formatters that ship with the Exception Handling Application Block. The application block includes the classes TextExceptionFormatter and XmlExceptionFormatter, both of which derive from the ExceptionFormatter class.

Creating a New Exception Formatter Class

This procedure describes the general steps you should take to create a custom formatter.

To create a new exception formatter class

  1. Create a new class that derives from ExceptionFormatter.
  2. Override the methods declared for formatting the exception information. The ExceptionFormatter class declares several abstract and virtual methods used for constructing formatted exception information. For a list and description of the methods that can be overridden, see ExceptionFormatter in the Reference section.

The following code shows a custom exception formatter that overrides the WriteStackTrace method to exclude stack trace information.

public class AppTextExceptionFormatter : TextExceptionFormatter
{
  public AppTextExceptionFormatter(TextWriter writer, Exception exception)
    : base (writer, exception) 
  {
  }

  protected override void WriteStackTrace(string stackTrace) 
  {
  }
}
'Usage
Public Class AppTextExceptionFormatter
    Inherits TextExceptionFormatter

  Public Sub New(ByVal writer As TextWriter, ByVal e As Exception)
        MyBase.new(writer, e)
    End Sub

    Protected Overrides Sub WriteStackTrac(ByVal stackTrace As String)
    End Sub

End Class
Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.