企业库 如何实现 IExceptionHandler?

发布于 2024-07-19 11:30:43 字数 1633 浏览 7 评论 0原文

我必须为企业库 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;
    }

}

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等风也等你 2024-07-26 11:30:43

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

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

使处理程序代码成为线程安全的是一个好主意,以防您必须在多个线程中处理异常(如果您在本项目中不需要,则可能需要在下一个项目中处理)。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文