在堆栈跟踪中获取 VB.NET 行号 [英] Getting VB.NET line numbers in stack trace

查看:40
本文介绍了在堆栈跟踪中获取 VB.NET 行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 VB.NET 2010 Winforms 应用程序,我想在其中在堆栈跟踪中包含行号.我已阅读以下问题和答案:

I have a VB.NET 2010 Winforms application where I'd like to include line numbers in the stack trace. I've read the following question and answers:

如何打印行在 VB.net 中运行应用程序期间的编号

其中提到您始终需要在代码中包含 PDB 文件,其中包含在此类情况下使用的调试信息".在高级编译器设置下,我已经尝试将生成调试信息"作为pdb-only"和full"用于我的发布版本,并确认在与我的 EXE 相同的目录中生成了一个新的 PDB 文件.但是,以下测试代码为每个堆栈帧生成行号为零且不返回文件名:

Which mentions "you always need to include the PDB file with your code, which contains debugging information that is used in situations like this". Under advanced compiler settings I've tried "Generate debug info" as "pdb-only" and "full" for my release build and confirmed that a fresh PDB file is generated in the same directory as my EXE. However the following test code generates a line number of zero for each stack frame and doesn't return a filename:

    Dim st As StackTrace = New StackTrace(ex)
    For Each sf As StackFrame In st.GetFrames
        MsgBox("Line " & sf.GetFileLineNumber() & sf.GetFileName)
    Next

然而,下面的代码在它生成一个看起来不错的堆栈跟踪之后直接生成,所以它似乎不是一般异常处理程序的问题:

However the following code straight after it generates an otherwise good looking stack trace so it doesn't seem to be a problem with the exception handler in general:

ExceptionDetails.Text = ex.GetType.ToString & "(0x" & hr.ToString("X8") & "): " & ex.Message & vbCrLf & ex.StackTrace

我似乎在项目配置下找不到任何其他可能的设置,想知道是否有人对可能导致此问题的其他事情有任何想法.我在此处和其他地方搜索到的所有解决方案似乎都建议确保 PDB 与可执行文件位于同一路径中.

I can't seem to find any other likely settings under the project configuration and wondered if anyone had ideas on other things that may cause this problem. All the solutions I've found searching here and elsewhere just seem to suggest making sure the PDB is in the same path as the executable.

推荐答案

来自 您正在调用的构造函数的文档:

StackTrace 是用调用者的当前线程创建的,不包含文件名、行号或列信息.

The StackTrace is created with the caller's current thread, and does not contain file name, line number, or column information.

尝试使用:

Dim st As StackTrace = New StackTrace(ex, True)

相反,它使用这个构造函数.第二个构造函数参数描述为:

instead, which uses this constructor. The second constructor parameter is described as:

true 捕获文件名、行号和列号;否则为假.

true to capture the file name, line number, and column number; otherwise, false.

这篇关于在堆栈跟踪中获取 VB.NET 行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆