如何记录具有完整调用堆栈的异常?
我想使用 ELMAH 记录异常(不将其一直抛出调用堆栈)并记录整个调用堆栈。
示例代码:
protected void Page_Load(object sender, EventArgs e)
{
DoSomething();
}
private void DoSomething()
{
try { TrySomething(); }
catch (Exception ex) { LogException(ex); }
}
private void TrySomething()
{
throw new NotImplementedException();
}
public static void LogException(Exception ex)
{
var currentStack = new System.Diagnostics.StackTrace(true);
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
}
现在,在 LogException 方法中,我可以看到调用堆栈告诉我 DoSomething() 调用了 TrySomething(),并且引发了异常,但我看不到调用堆栈显示 Page_Load() 调用了 DoSomething()。我希望能够看到完整的调用堆栈。
ex.StackTrace 在 LogException 方法中的示例:
at WebApplication1._Default.TrySomething() in C:\Projects\test\GeneralTests\WebApplication1\Default.aspx.cs:line 26
at WebApplication1._Default.DoSomething() in C:\Projects\test\GeneralTests\WebApplication1\Default.aspx.cs:line 20
我可以从 System.Diagnostics.StackTrace() 获取完整的调用堆栈,例如:(
at WebApplication1._Default.LogException(Exception ex)
at WebApplication1._Default.DoSomething()
at WebApplication1._Default.Page_Load(Object sender, EventArgs e)
[snip]
并且我可以通过遍历 StackTrace 的每一帧来获取行号和源文件详细信息)
但是如何将其注入到异常中或使用此调用堆栈详细信息引发新的异常?有没有一种优雅的方法来做到这一点?我错过了一些非常明显的事情吗?
I want to use ELMAH to log an exception (without throwing it all the way up the call stack) and it log the entire call stack.
Example code:
protected void Page_Load(object sender, EventArgs e)
{
DoSomething();
}
private void DoSomething()
{
try { TrySomething(); }
catch (Exception ex) { LogException(ex); }
}
private void TrySomething()
{
throw new NotImplementedException();
}
public static void LogException(Exception ex)
{
var currentStack = new System.Diagnostics.StackTrace(true);
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
}
Now, within the LogException method I can see the call stack telling me DoSomething() called TrySomething(), and that threw the exception, but I can't see the call stack showing me Page_Load() called DoSomething(). I want to be able to see the full calling stack.
Example of what ex.StackTrace looks like inside LogException method:
at WebApplication1._Default.TrySomething() in C:\Projects\test\GeneralTests\WebApplication1\Default.aspx.cs:line 26
at WebApplication1._Default.DoSomething() in C:\Projects\test\GeneralTests\WebApplication1\Default.aspx.cs:line 20
I can get the full call stack from System.Diagnostics.StackTrace(), for example:
at WebApplication1._Default.LogException(Exception ex)
at WebApplication1._Default.DoSomething()
at WebApplication1._Default.Page_Load(Object sender, EventArgs e)
[snip]
(and I can get line numbers and source file details by walking each frame of StackTrace)
But how do I inject this into the Exception or raise a new Exception with this call stack detail? Is there an elegant way to do this? Have I missed something really obvious?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当发生错误时,您可以执行以下操作来获取完整堆栈:
You can do something like this to get the full stack when an error ocurrs:
当应用程序在没有调试标志的情况下编译时,堆栈不一定会在执行中保留。你只能通过在每个方法中添加try/catch来保证每一项都被记录下来。
When the app is compiled without debug flags, the stack is not necessarily preserved in execution. You can only guarantee each item being recorded by adding try / catch to every method.
您看过 Exception.ToString() 的结果吗?它包括堆栈跟踪。它包含您正在寻找的类型吗?
Have you looked at the results of Exception.ToString()? It includes a stack trace. Does it include the kind that you're looking for?