为什么我从Exceptions创建的堆栈跟踪中没有获得行号?

时间:2010-02-09 12:17:28

标签: .net debugging stack-trace line-numbers

好;假设此代码在调试模式下运行 -

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属性中的行号。我假设这意味着我拥有我需要的一切。

1 个答案:

答案 0 :(得分:16)

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

  

使用。创建StackTrace   调用者的当前线程,而不是   包含文件名,行号或   栏目信息。

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

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