c# – AggregateException.Flatten()丢失堆栈跟踪信息.丢失的信息是否重要?

c# – AggregateException.Flatten()丢失堆栈跟踪信息.丢失的信息是否重要?,第1张

概述AggregateException.Flatten方法的文档 on MSDN说明如下:“如果任何内部异常本身是AggregateException的实例,则此方法将以递归方式展平所有这些异常.新AggregateException中返回的内部异常将是所有内部异常的并集.来自异常树的内部异常,以提供的AggregateException实例为根.“ 这听起来很可疑,就像我们将丢失堆栈跟踪信息一样, AggregateException.Flatten方法的文档 on MSDN说明如下:“如果任何内部异常本身是AggregateException的实例,则此方法将以递归方式展平所有这些异常.新AggregateException中返回的内部异常将是所有内部异常的并集.来自异常树的内部异常,以提供的AggregateException实例为根.“

这听起来很可疑,就像我们将丢失堆栈跟踪信息一样,所以我看一下实现on Reference Source,看起来堆栈跟踪信息确实会丢失.

所以我们进行了测试:

try{    try    {        try        {            throw new InvalIDOperationException();        }        catch (Exception e)        {            try            {                throw new FormatException();            }            catch (Exception e2)            {                throw new AggregateException(e,e2);            }        }    }    catch (Exception e)    {        try        {            throw new NotImplementedException();        }        catch (Exception e2)        {            throw new AggregateException(e,e2);        }    }}catch (Exception e){    file.AppendAllText(@"C:\tmp.txt",e.ToString());    file.AppendAllText(@"C:\tmp.txt",((AggregateException)e).Flatten().ToString());}

测试结果如下:

System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or more errors occurred. ---> system.invalIDOperationException: Operation is not valID due to the current state of the object.   at ConsoleApplication6.Program.Main(String[] args) in C:\Projects\ConsoleApplication6\ConsoleApplication6\Program.cs:line 29   --- End of inner exception stack trace ---   at ConsoleApplication6.Program.Main(String[] args) in C:\Projects\ConsoleApplication6\ConsoleApplication6\Program.cs:line 39   --- End of inner exception stack trace ---   at ConsoleApplication6.Program.Main(String[] args) in C:\Projects\ConsoleApplication6\ConsoleApplication6\Program.cs:line 51---> (Inner Exception #0) System.AggregateException: One or more errors occurred. ---> system.invalIDOperationException: Operation is not valID due to the current state of the object.   at ConsoleApplication6.Program.Main(String[] args) in C:\Projects\ConsoleApplication6\ConsoleApplication6\Program.cs:line 29   --- End of inner exception stack trace ---   at ConsoleApplication6.Program.Main(String[] args) in C:\Projects\ConsoleApplication6\ConsoleApplication6\Program.cs:line 39---> (Inner Exception #0) system.invalIDOperationException: Operation is not valID due to the current state of the object.   at ConsoleApplication6.Program.Main(String[] args) in C:\Projects\ConsoleApplication6\ConsoleApplication6\Program.cs:line 29<------> (Inner Exception #1) System.FormatException: One of the IDentifIEd items was in an invalID format.   at ConsoleApplication6.Program.Main(String[] args) in C:\Projects\ConsoleApplication6\ConsoleApplication6\Program.cs:line 35<---<------> (Inner Exception #1) System.NotImplementedException: The method or operation is not implemented.   at ConsoleApplication6.Program.Main(String[] args) in C:\Projects\ConsoleApplication6\ConsoleApplication6\Program.cs:line 47<---System.AggregateException: One or more errors occurred. ---> System.NotImplementedException: The method or operation is not implemented.   at ConsoleApplication6.Program.Main(String[] args) in C:\Projects\ConsoleApplication6\ConsoleApplication6\Program.cs:line 47   --- End of inner exception stack trace ------> (Inner Exception #0) System.NotImplementedException: The method or operation is not implemented.   at ConsoleApplication6.Program.Main(String[] args) in C:\Projects\ConsoleApplication6\ConsoleApplication6\Program.cs:line 47<------> (Inner Exception #1) system.invalIDOperationException: Operation is not valID due to the current state of the object.   at ConsoleApplication6.Program.Main(String[] args) in C:\Projects\ConsoleApplication6\ConsoleApplication6\Program.cs:line 29<------> (Inner Exception #2) System.FormatException: One of the IDentifIEd items was in an invalID format.   at ConsoleApplication6.Program.Main(String[] args) in C:\Projects\ConsoleApplication6\ConsoleApplication6\Program.cs:line 35<---

请注意,在展平的AggregateException中没有提到第39行!

丢失的信息是否重要?或者有没有理由认为,当我们压平AggregateException时,我们会丢失第39行?

解决方法 如果我正确理解了您的示例,那么您将说丢失了抛出AggregateException时创建的堆栈跟踪.这是预期的,因为Flatten()将过滤掉属于InnerExceptions层次结构的任何AggregateExceptions.我不会说信息丢失,因为在大多数情况下,有用的信息将在非AggregateException实例中. 总结

以上是内存溢出为你收集整理的c# – AggregateException.Flatten()丢失堆栈跟踪信息.丢失的信息是否重要?全部内容,希望文章能够帮你解决c# – AggregateException.Flatten()丢失堆栈跟踪信息.丢失的信息是否重要?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1231216.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存