如何记录具有完整调用堆栈的异常?

发布于 2024-09-26 14:43:25 字数 1404 浏览 2 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

逆蝶 2024-10-03 14:43:25

当发生错误时,您可以执行以下操作来获取完整堆栈:

var currentStack = new System.Diagnostics.StackTrace(true);
return currentStack.ToString();

You can do something like this to get the full stack when an error ocurrs:

var currentStack = new System.Diagnostics.StackTrace(true);
return currentStack.ToString();
静待花开 2024-10-03 14:43:25

当应用程序在没有调试标志的情况下编译时,堆栈不一定会在执行中保留。你只能通过在每个方法中添加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.

可遇━不可求 2024-10-03 14:43:25

您看过 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文