捕获所有内部异常详细信息的最佳实践是什么? [英] What is the best practice for capturing all inner exception details?

查看:112
本文介绍了捕获所有内部异常详细信息的最佳实践是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

记录包括所有可能的内部异常在内的完整异常详细信息的最佳实践是什么?

What is the best practice for logging complete exception details including all possible inner exceptions?

当前,我使用以下代码:

Currently, I use the following code:

try
{
    //some code that throws an exception
}
catch(Exception ex)
{
    do
    {
        Console.WriteLine(ex.Message+ex.StackTrace);
        ex=ex.InnerException;
    }while(ex!=null)
}

是否存在任何情况

推荐答案

您是否尝试过仅使用 ex.ToString()?它提供了您需要诊断的大多数(如果不是全部)数据,包括消息详细信息,堆栈跟踪和内部异常:

Have you tried just using ex.ToString()? It gives most (if not all) of the data you need to diagnose - including the message details, stack trace, and inner exceptions:

来自 MSDN


ToString返回当前异常的表示形式,该异常是人类希望理解的
。如果异常包含
区域性敏感数据,则ToString
返回的字符串表示形式需要考虑当前的系统区域性。尽管
对于返回的字符串的格式没有确切要求,但
应该尝试反映用户
所感知的对象的值。 ToString的默认实现获取引发当前异常的类的名称
,消息,内部异常上调用ToString的
结果以及调用
Environment.StackTrace的结果。如果这些成员中的任何一个为null,则其值为
,不包含在返回的字符串中。

ToString returns a representation of the current exception that is intended to be understood by humans. Where the exception contains culture-sensitive data, the string representation returned by ToString is required to take into account the current system culture. Although there are no exact requirements for the format of the returned string, it should attempt to reflect the value of the object as perceived by the user. The default implementation of ToString obtains the name of the class that threw the current exception, the message, the result of calling ToString on the inner exception, and the result of calling Environment.StackTrace. If any of these members is null, its value is not included in the returned string.

这篇关于捕获所有内部异常详细信息的最佳实践是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆