Exception.Message VS Exception.ToString() [英] Exception.Message vs Exception.ToString()

查看:467
本文介绍了Exception.Message VS Exception.ToString()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code正在记录 Exception.Message 。但是,我读到一篇文章,其中指出,最好是使用 Exception.ToString()。对于后者,可以保留更多的错误重要信息。

I have code that is logging Exception.Message. However, I read an article which states that it's better to use Exception.ToString(). With the latter, you retain more crucial information about the error.

这是真实的,是安全的继续和替换所有code记录 Exception.Message

Is this true, and is it safe to go ahead and replace all code logging Exception.Message?

我还使用了 log4net的的基于XML的布局。难道 Exception.ToString()可能包含无效的XML字符,这可能会导致问题?

I'm also using an XML based layout for log4net. Is it possible that Exception.ToString() may contain invalid XML characters, which may cause issues?

推荐答案

<一个href="http://msdn.microsoft.com/en-us/library/system.exception.message.aspx"><$c$c>Exception.Message只包含与异常关联的消息(DOH)。例如:

Exception.Message contains only the message (doh) associated with the exception. Example:

对象引用不设置到对象的实例

Object reference not set to an instance of an object

的<一个href="http://msdn.microsoft.com/en-us/library/system.exception.tostring.aspx"><$c$c>Exception.ToString()方法会给出一个更详细的输出,又包含异常类型,邮件(来自前),堆栈跟踪,而所有这些东西嵌套/内部异常。更多precisely,该方法返回如下:

The Exception.ToString() method will give a much more verbose output, containing the exception type, the message (from before), a stack trace, and all of these things again for nested/inner exceptions. More precisely, the method returns the following:

的ToString返回当前异常,旨在用人类可理解的重新presentation。其中除包含文化敏感的数据,该字符串通过再返回的ToString presentation需要考虑当前系统区域性。虽然有对返回的字符串的格式没有具体要求,应该尝试以反映对象的值作为用户感知。

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.

的ToString的默认实现获取抛出当前异常的消息,调用toString对内部异常的结果,并呼吁Environment.StackTrace的结果类的名称。如果这些成员为空引用(在Visual Basic中为Nothing),它的价值不包括在返回的字符串中

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 a null reference (Nothing in Visual Basic), its value is not included in the returned string.

如果没有错误消息,或者如果它是一个空字符串(),则没有错误消息被返回。内部异常和堆栈跟踪的名称是,只有当他们没有空引用(在Visual Basic中为Nothing)返回。

If there is no error message or if it is an empty string (""), then no error message is returned. The name of the inner exception and the stack trace are returned only if they are not a null reference (Nothing in Visual Basic).

这篇关于Exception.Message VS Exception.ToString()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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