在企业库中,我没有将足够的详细信息放入日志中,因此我开始编写此处理程序以提取异常特定属性并将它们添加到消息字符串中:
[ConfigurationElementType(typeof(CustomHandlerData))]
public class ExposeDetailExceptionHandler : IExceptionHandler
{
public Exception HandleException(Exception exception, Guid handlingInstanceId)
{
if (exception is System.Net.WebException)
return ExposeDetail((System.Net.WebException)exception);
if (exception is System.Web.Services.Protocols.SoapException)
return ExposeDetail((System.Web.Services.Protocols.SoapException)exception);
return exception;
}
private Exception ExposeDetail(System.Net.WebException Exception)
{
string details = "";
details += "System.Net.WebException: " + Exception.Message + Environment.NewLine;
details += "Status: " + Exception.Status.ToString() + Environment.NewLine;
return new Exception(details, Exception);
}
private Exception ExposeDetail(System.Web.Services.Protocols.SoapException Exception)
{
//etc
}
}
(顺便说一句,有没有更好的方法来选择运行哪个版本的 ExposeDetail?)
这是记录这些详细信息的最佳或可接受的方式吗?我最初的想法是我应该实现一个 ExceptionFormatter 但这似乎要简单得多。