views:

514

answers:

1

I have to implement a IExceptionHandler for the Enteprise Library 4.1. In my particular case I want to use it to log the exception to Fogbugz but the inner details is not what I am asking about. What I need is how to - best practicies - implement it, How to get the config for a app.config or web.config. etc.

I have code This so far:

   public class LcpFogbugzExceptionHandler : IExceptionHandler {
   /// <summary>
   /// Initializes a new instance of the <see cref="LcpFogbugzExceptionHandler"/> class.
   /// </summary>
   public LcpFogbugzExceptionHandler() {
        // <param name="ignore">The ignore.</param>
        //NameValueCollection ignore
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="T:LcpFogbugzExceptionHandler"/> class.
    /// </summary>
    /// <param name="ignore">The ignore.</param>
   public LcpFogbugzExceptionHandler(NameValueCollection ignore) {
   }

    /// <summary>
    /// Handles the exception.
    /// </summary>
    /// <param name="exception">The exception.</param>
    /// <param name="handlingInstanceId">The handling instance id.</param>
    /// <returns></returns>
   [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1300:SpecifyMessageBoxOptions"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.Convert.ToBoolean(System.String)")]
    public Exception HandleException(Exception exception, Guid handlingInstanceId) {
        // Perform processing here. The exception returned will be passed to the next
        // exception handler in the chain. 

        return exception;
    }

}
+1  A: 

I'm not sure what you mean by best practices - you have the code skeleton correct so just fill it with your implementation. Random points that might be useful:

You can read config settings in the normal way (ConfigurationManager etc). Your handler is running in the same process and thread that called ExceptionPolicy.HandleException in the first place.

It would be a good idea to make the handler code thread-safe, in case you have to handle exceptions in multiple threads (and if you don't in this project, you might need to in the next one).

If you want to pass any non-static instance data into the handler from the calling code, you could populate the Data dictionary of the exception being handled, which will be preserved intact through the whole handler chain and out to the calling code again. I have used this technique to send data into a handler, get data back out of a handler, and allow one handler to control the actions of the next one in the chain. If you do this, make sure that all Data values are serializable.

Christian Hayter