Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
From the author of

The EMAB defines a default publisher, the EventLog. Using the EMAB to log an exception is accomplished by adding a reference to the Microsoft.ApplicationBlocks. ExceptionManagement.dll and Microsoft.ApplicationBlocks. ExceptionManagement. Interfaces.dll (downloadable from Microsoft) and invoking the static method ExceptionManager.Publish, passing an instance of an exception object to the Publish method. As a consumer, the implementation details are concealed from you.

By default, if you publish an exception, it is written to the system EventLog. The Console application in Listing 1 demonstrates how easy it is to log exceptions using the EMAB.

Listing 1 Log exceptions that occur in your application.

using System;
using Microsoft.ApplicationBlocks.ExceptionManagement;

namespace EmabDemo
{
  class Class1
  {
    [STAThread]
    static void Main(string[] args)
    {
      try
      {
        // write some code that and an error is thrown
       throw new Exception("Ooops!");
      }
      catch(Exception ex)
      {
        ExceptionManager.Publish(ex);
        throw; // re-throw if desired
      }
    }
  }
}

A basic rule of thumb is that if you catch an exception, log it. After logging an exception you can decide to propagate the exception—rethrow it—degrade and shut down, or ideally, handle the exception and let your application continue. Deceptively simple and unobtrusively everyone calls the same method—Publish—passing an exception, regardless of where the exception is logged.

  • + Share This
  • 🔖 Save To Your Account