企业库如何实现IExceptionHandler?

时间:2009-05-04 19:40:42

标签: .net exception-handling fogbugz enterprise-library

我必须为Enteprise Library 4.1实现一个IExceptionHandler。在我的特定情况下,我想用它来将异常记录到Fogbugz,但内部细节不是我要问的。我需要的是如何 - 最佳实践 - 实现它,如何获取app.config或web.config的配置。等

我有代码到目前为止:

   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 个答案:

答案 0 :(得分:2)

我不确定你的最佳做法是什么意思 - 你的代码框架是正确的,所以只需填写你的实现。随机点可能有用:

您可以按正常方式(ConfigurationManager等)阅读配置设置。您的处理程序在首先调用ExceptionPolicy.HandleException的同一进程和线程中运行。

最好让处理程序代码是线程安全的,以防你必须处理多个线程中的异常(如果你不在这个项目中,你可能需要在下一个程序中)。 / p>

如果要将任何非静态实例数据从调用代码传递到处理程序中,可以填充正在处理的异常的Data字典,该字典将通过整个处理程序链完整保留并输出再次调用代码。我已经使用这种技术将数据发送到处理程序,从处理程序中取出数据,并允许一个处理程序控制链中下一个的操作。如果这样做,请确保所有Data值都可序列化。