使用Microsoft Enterprise Library异常处理程序检索参数值

时间:2010-10-05 15:12:37

标签: .net-4.0 enterprise-library

我们正在使用Enterprise Library来满足我们应用中的所有日志记录和异常处理需求。我们添加了一个电子邮件侦听器,可以将所有捕获的例外电子邮件发送给管理员。一个要求是当方法中发生异常时,我们需要检索方法的参数值(如果有)并将其附加到发送的电子邮件中的异常详细信息中。是否可以不编写自定义记录器?

1 个答案:

答案 0 :(得分:0)

只需创建自定义异常,将消息设置为参数:

try {
  ...
} catch(Exception ex) {
  var customException = new CustomException(ex, string.format("Param1 {0}, Param2 {1}", param1, param2));
  bool rethrow = ExceptionPolicy.HandleException(customException, PolicyName);
}

验证了ExceptionFormatter类实际上会遍历所有内部异常,所以你的CustomException可以像

一样简单
public class CustomException : Exception
{
    public CustomException(string message, Exception innerException) : base(message, innerException)
    {
    }
}