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

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

问题描述

我有代码记录 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.

这是真的,可以安全地替换所有代码记录 Exception.Message

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

我还使用基于XML的布局来 log4net 。可能 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?

推荐答案

异常。消息 仅包含与异常关联的消息(doh)。示例:

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


对象引用未设置为对象的实例

Object reference not set to an instance of an object

异常.ToString() 方法将给出一个更加详细的输出,包含异常类型,消息(从前),堆栈跟踪,以及所有这些东西再次为嵌套/内部例外。更准确地说,该方法返回以下内容:

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返回一个意图被人理解的当前异常的表示。如果异常包含文化敏感数据,则ToString返回的字符串表示需要考虑当前的系统文化。虽然对于返回的字符串的格式没有确切的要求,但它应该尝试反映用户感觉到的对象的值。

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天全站免登陆