C# 捕获所有内部异常详细信息的最佳实践是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18489387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the best practice for capturing all inner exception details?
提问by CodeWarrior
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)
}
Are there any scenarios where this code may fail?
是否有任何情况下此代码可能会失败?
采纳答案by D Stanley
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:
你试过只使用ex.ToString()
吗?它提供了您需要诊断的大部分(如果不是全部)数据 - 包括消息详细信息、堆栈跟踪和内部异常:
From MSDN:
从MSDN:
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.
ToString 返回旨在被人类理解的当前异常的表示。如果异常包含区域性敏感数据,则需要 ToString 返回的字符串表示形式将当前系统区域性考虑在内。虽然对返回字符串的格式没有确切的要求,但它应该尝试反映用户感知的对象的值。ToString 的默认实现获取抛出当前异常的类的名称、消息、对内部异常调用 ToString 的结果以及调用 Environment.StackTrace 的结果。如果这些成员中的任何一个为空,则其值不包含在返回的字符串中。
回答by Maarten
I have this extension method which suits my purposes just fine.
我有这个扩展方法很适合我的目的。
public static class ExceptionExtensions {
public static string ToMessageAndCompleteStacktrace(this Exception exception) {
Exception e = exception;
StringBuilder s = new StringBuilder();
while (e != null) {
s.AppendLine("Exception type: " + e.GetType().FullName);
s.AppendLine("Message : " + e.Message);
s.AppendLine("Stacktrace:");
s.AppendLine(e.StackTrace);
s.AppendLine();
e = e.InnerException;
}
return s.ToString();
}
}
And use it like this:
并像这样使用它:
using SomeNameSpaceWhereYouStoreExtensionMethods;
try {
// Some code that throws an exception
}
catch(Exception ex) {
Console.WriteLine(ex.ToMessageAndCompleteStacktrace());
}
Update
更新
Since I'm receiving upvotes for this answer I want to add that I stopped using this extension method, and now I'm just using exception.ToString()
. It gives more information. So please, stop using this method, and just use .ToString()
. See the answer above.
由于我收到了对此答案的赞成票,因此我想补充一点,我已停止使用此扩展方法,现在我只使用exception.ToString()
. 它提供了更多信息。所以请停止使用这种方法,而只使用.ToString()
. 请参阅上面的答案。