How do I implement an IExceptionHandler?

I need to implement IExceptionHandler for Enteprise 4.1 library. In my specific case, I want to use it to log an exception to Fogbugz, but the internal details are not what I'm asking for. I need how - best practices - to implement it, How to get the config for app.config or web.config. and etc.

I have the code This is for now:

   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;
    }

}

      

0


source to share


1 answer


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

You can read the config settings in the usual way ( ConfigurationManager

etc.). Your handler is running in the same process and thread that it called ExceptionPolicy.HandleException

in the first place.



It would be nice to make the handler code thread-safe if you have to handle exceptions across multiple threads (and if you're not in this project, you might need to do the next one).

If you want to pass any non-static instance data to the handler from the calling code, you can fill the dictionary of the Data

handled exception, which will be saved unchanged through the whole chain of handlers and exit the code call again. I used this method to send data to a handler, receive data from a handler, and allows one handler to control the actions of the next in the chain. If you do, make sure all values Data

are serializable.

+2


source







All Articles