为什么 .NET 不记录 StackOverflow 异常的堆栈跟踪?
如果我这样写:
class Program
{
static void Main(string[] args)
{
throw new Exception("lol");
}
}
并从命令行运行 exe,我会在事件日志中看到两个条目。一个是应用程序错误,表明存在未处理的异常,另一个包含源为 .NET 运行时的堆栈跟踪。
如果我这样写:
class Program
{
static void Main(string[] args)
{
Recurse4Evr();
}
static void Recurse4Evr()
{
Recurse4Evr();
}
}
我的事件日志中只会出现一个条目,其中显示“应用程序错误”并且存在堆栈溢出异常。堆栈跟踪中没有第二个条目,因此它基本上没有用。
为什么不记录堆栈跟踪?如果我设置 DebugDiag 并将其附加到我的进程,然后出现堆栈溢出,则 DebugDiag 能够记录堆栈跟踪。显然,堆栈跟踪可以通过某种方式供外界使用。如果运行时由于检测到堆栈溢出而终止进程,那么它也知道堆栈是什么。
在具有许多复杂交互的大型应用程序中,通常不可能重新创建导致堆栈溢出的条件。在这种情况下,堆栈跟踪是弄清楚发生了什么情况的唯一方法。为什么微软认为记录这些信息并不重要?是否存在不明显的合理设计决策?
If I write this:
class Program
{
static void Main(string[] args)
{
throw new Exception("lol");
}
}
and run the exe from the command line, I get two entries in my event log. One is an application error that says there was an unhandled exception, and the other contains the stack trace with the source as .NET Runtime.
If I write this:
class Program
{
static void Main(string[] args)
{
Recurse4Evr();
}
static void Recurse4Evr()
{
Recurse4Evr();
}
}
I only get one entry in my event log, which says Application Error and that there was a stack overflow exception. There isn't that second entry with the stack trace, so it's basically useless.
Why isn't the stack trace also logged? If I setup DebugDiag and attach it to my process and then there is a stack overflow, DebugDiag is able to log the stack trace. Obviously the stack trace is available to the outside world in some way. If the runtime is terminating the process because it detected a stackoverflow, then it also knows what the stack is.
In large applications that have many complex interactions, it is often not possible to recreate the conditions that led to a stack overflow. In this situation, a stack trace is the only way to figure out what happened. Why did microsoft decide it wasn't important to log this information? Was there a legitimate design decision that isn't obvious?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
堆栈溢出被认为是不可恢复的情况,因此运行时会终止进程。
总结:
来自 Damien_The_Unknowner
来自 StackOverflowExceptions:
来自 JaredPar
从 2.0 开始,只能捕获 StackOverflow 异常在以下情况下。
Stack overflow is considered a non-recoverable situation and for such the runtime kills the process.
to sum up:
from Damien_The_Unbeliever
From the MSDN page on StackOverflowExceptions:
from JaredPar
Starting with 2.0 a StackOverflow Exception can only be caught in the following circumstances.
这显然是不可能的。如果您想要堆栈跟踪以便找到导致应用程序终止的违规代码,则需要附加第 3 方工具,例如 DebugDiag 或 AdsPlus。祝你好运,这些工具基本上没有文档。
This is apparently not possible. If you want a stack trace so you can find the offending code that killed your application, you need to attach a 3rd party tool like DebugDiag or AdsPlus. Good luck, there is basically no documentation for these tools.