好的;假设此代码在 Debug模式下运行-

static StackFrame GetTopFrameWithLineNumber(Exception e)
{
    StackTrace trace = new StackTrace(e);
    foreach (StackFrame frame in trace.GetFrames())
    {
        if (frame.GetFileLineNumber() != 0)
        {
            return frame;
        }
    }
    return null;
}

我总是返回null。为什么当我检查Exception.StackTrace字符串时,堆栈框架没有行号,但对于任何非框架代码,它们显然都具有行号吗?从我不知道的异常构造堆栈跟踪是否存在一些问题?

编辑清晰度:
在抛出的异常中,我可以在StackTrace属性中看到行号。我假设这意味着我还有其他需要的东西。

最佳答案

根据documentation on the StackTrace constructor overload that takes an exception,以这种方式创建StackTrace时不能指望行号。



要获取行号,您需要使用the overload that takes a bool as well as the exception

您还需要符号文件(pdb)作为行号。符号文件可用于调试和发行版本。

关于.net - 为什么我从异常创建的堆栈跟踪中找不到行号?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2228893/

10-11 22:19
查看更多