在 try..catch 中获取 Exception 对象以包含完整的堆栈跟踪,目前它已被截断
我试图在 try..catch 的 catch 中获取完整的堆栈跟踪。目前它已被截断为仅包含出现错误的当前方法。
让我解释一下。目前我的堆栈跟踪包括发生错误的方法“第三”,但不包括第一和第二,我相信这是设计使然。
private void First()
{
this.Second();
}
private void Second()
{
this.Third();
}
private void Third()
{
try
{
throw new SystemException("ERROR HERE!");
}
catch (Exception ex)
{
// I WILL LOG THE EXCEPTION object "EX" here ! but ex.StackTrace is truncated!
}
}
我已经看到了许多在 STRING 中获取完整堆栈跟踪的技巧,但问题是我的日志记录框架需要一个“Exception”类型的对象,但我的异常变量(ex)是有效的,但属性 StackTrace 被截断。
无论如何,我是否可以收到带有完整堆栈跟踪的完整异常,这样我仍然可以发送我的“EX”,但这次它将有一个未截断的堆栈跟踪。
UnhandledError 事件似乎工作得就像我到达这里一样,异常有一个堆栈跟踪并且已完全填充。
I am trying to get the full stack trace when within a catch of try..catch. Currently its truncated to include only the current method where the error is.
Let me explain. Currently my stack trace includes the method "Third" where the error happens but First and Second isn't included, I believe this is by design.
private void First()
{
this.Second();
}
private void Second()
{
this.Third();
}
private void Third()
{
try
{
throw new SystemException("ERROR HERE!");
}
catch (Exception ex)
{
// I WILL LOG THE EXCEPTION object "EX" here ! but ex.StackTrace is truncated!
}
}
I have seen a number of tricks to get the full stack trace in a STRING but problem is my logging framework expects a object of type "Exception", but my variable (ex) which has my exception is valid but the property StackTrace is truncated.
Is there anyway I can receive a FULL exception with a FULL stack trace so I am able to still send along my "EX" but this time it will have an Untruncated stack trace.
UnhandledErrror event seems to work as if I arrive here the Exception has a stack trace and is fully populated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当前方法可能会添加两次,如果是这样,请从属性之一中删除一行。
The current method may be added two times, if so, remove one line from one of the properties.
这种情况下的输出是:
更新:我突然产生了疑问,并浏览了迄今为止提出的所有解决方案。它们都可以工作,尽管 Environment.StackTrace 是最简单的。输出如下所示:
The output in this case is:
Update: I suddenly had doubts, and ran through all the solutions presented thus far. They all work, though Environment.StackTrace is the easiest. Here's what the output looks like:
您不了解堆栈跟踪如何工作。异常跟踪中包含的是“路线”,简而言之,异常已到达您正在处理它的位置。
因此,在您的示例中,它根本还没有移动,它仍然处于“第三”方法中。如果它会冒泡到“第二”,那么它会穿过第三,到达第二,并且您将在堆栈跟踪中得到它。
我认为您真正想要的是 CallStack。是相同的概念,但是异常中包含的跟踪实际上是“向后”包含的,因为异常“向上”移动到处理它的位置。
尝试记录处理异常的 CallStack。
You don't understand how stack trace work. What is included in an exception trace is the "route" short to speak that the exception has traveled to reach the point where you're handling it.
Thus, on your example, it hasn't traveled at all yet, it's still in the "Third" method. If it would bubble up to "Second", then it would have gone across Third, to Second, and you'll be getting that on the stack trace.
I think what you actually want is the CallStack. Is the same concept, but the trace that's included in the exception is actually included "backwards" sort to speak, as the exception travels "up" to the place where it is handled.
Try logging the CallStack where you handle the exception.
您需要这样的东西:
查看这篇文章以获取更多信息:
StackTrace 类
希望这会有所帮助。
You need something like this :
Check this article for more info :
StackTrace class
Hope this helps.