如何获取异常详细信息

Obautista

我有一个WCF服务,其中已实现了自定义服务错误。我根据特定条件抛出错误,因此在if语句中将错误抛出以下。

throw new FaultException<CustomServiceFault>(
    new CustomServiceFault
    {
        ErrorMessage = string.Format(
            "Error in response, code:{0} message:{1}", 
             response.Error.Code, 
             response.Error.Message),
        Source = "ConnectToOpenLdap Method",
        StackTrace = string.Format(
            "Error in response, code:{0} message:{1}", 
            response.Error.Code, 
            response.Error.Message)                                     
    },
    new FaultReason(
        string.Format(CultureInfo.InvariantCulture, "{0}", "Service fault exception")));

在捕获中,我抛出了这样的异常:

catch (Exception exception)
{
    var customServiceFault = GetCustomException(exception);
    throw new FaultException<CustomServiceFault>(
        customServiceFault, 
        new FaultReason(customServiceFault.ErrorMessage), 
        new FaultCode("Sender"));              
}   

GetCustomException()方法只是将异常转换为我的自定义异常对象。

问题是传递给GetCustomException()的异常在InnerException属性中没有详细信息。我看到的屏幕截图:

在此处输入图片说明

在if条件中引发异常时,如何提取或获取我设置的自定义ErrorMessage,Source等?正如您在屏幕快照中所看到的,展开的“ exception”显示了对象类型(我相信),并且在“ Detail”的内部显示了ErrorMessage,InnerExceptionMesage,Source和StackTrace。这就是我所追求的。如何在GetCustomException()方法中提取这些值?

这是GetCustomException()方法:

private static CustomServiceFault GetCustomException(Exception exception)
{                       
    var customServiceFault = new CustomServiceFault
    {
        ErrorMessage = exception.Message,
        Source = exception.Source,
        StackTrace = exception.StackTrace,
        Target = exception.TargetSite.ToString()
    };    

    return customServiceFault;
}

CustomServiceFault类:

[DataContract]
public class CustomServiceFault
{
    [DataMember]
    public string ErrorMessage { get; set; }
    [DataMember]
    public string StackTrace { get; set; }
    [DataMember]
    public string Target { get; set; }
    [DataMember]
    public string Source { get; set; }
    [DataMember]
    public string InnerExceptionMessage { get; set; }
}
仇杀

您没有收到InnerExceptionMessage,因为您没有在任何地方设置它。

private static CustomServiceFault GetCustomException(Exception exception)
{                       
    var customServiceFault = new CustomServiceFault
    {
        ErrorMessage = exception.Message,
        Source = exception.Source,
        StackTrace = exception.StackTrace,
        Target = exception.TargetSite.ToString(),

        // You should fill this property with details here.
        InnerExceptionMessage = exception.InnerException.Message;
    };    

    return customServiceFault;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从PyYAML异常获取详细信息?

如何获取运行时错误的完整信息而不是<超时获取异常详细信息>?

如何获取Windows文件详细信息?

如何获取Facebook Post详细信息?

如何从API获取版本详细信息

如何获取WooCommerce订单详细信息

如何获取推文的详细信息?

如何获取Java文件的“详细信息”

如何从班级中获取详细信息

如何使用Hangfire获取失败的后台作业的异常详细信息

如何从方法获取异常详细信息和方法返回的值

抛出异常的详细信息

Oracle异常详细信息

从Azure Worker角色获取异常详细信息

在 IIS 上发布后如何查看异常的详细信息

如何对未处理的异常使用“问题详细信息”响应?

如何从详细信息页面Angular 9中的单个对象(本地JSON)获取详细信息

如何在Android中获取“当前周”详细信息以及“季度详细信息”?

tcpdump:tcpdump如何获取主机名详细信息?

如何从C程序获取NIC详细信息?

如何从json api获取特定用户详细信息

如何实现PlaceAutocompleteFragment和PlaceAutocompleteActivity以获取位置详细信息

如何从img文件获取内核详细信息

如何通过Rails中的关联获取详细信息?

如何从 ExtentReport 获取测试步骤的详细信息

单击AppointmentPane时如何获取约会详细信息-JFXtras

重定向时如何获取表行详细信息

Powershell:如何获取防病毒产品详细信息

阅读提要,我如何从中获取详细信息