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

发布于 2024-11-03 20:12:54 字数 408 浏览 0 评论 0原文

我想在 VB.net 应用程序中打印出带有行号的调试消息。 我确实喜欢这样做,

Dim st As StackTrace
Dim sf As StackFramee
st = New StackTrace(New StackFrame(True))
sf = st.GetFrame(0)
Console.WriteLine.("Line " & sf.GetFileLineNumber())

我想将代码片段放入一个类中,每次调用 logMsg 方法以在源代码中使用行号记录我的消息时。 但我发现如果我将上面的代码片段放入一个类中,行号总是相同的,这就是我新添加的“st”行。

该功能与C++中的_LINE宏完全相同。其实我是C++程序员。

无论如何要解决这个问题吗?谢谢。

I would like to print out my debug message with line number in VB.net application.
I did like this,

Dim st As StackTrace
Dim sf As StackFramee
st = New StackTrace(New StackFrame(True))
sf = st.GetFrame(0)
Console.WriteLine.("Line " & sf.GetFileLineNumber())

I wanna put the snippet to a class, everytime I call logMsg method to log my message with line number in source code.
But I found if I put the snippet above into a class, the line number was always same, that's the line which I new 'st'.

The function is exact same with _LINE macro in C++. Actually I am C++ programmer.

Anyway to fix this problem? thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

我乃一代侩神 2024-11-10 20:12:54

您所显示的代码完全按照预期工作。它正在打印捕获堆栈帧的行号。因为您已在不同的类中定义了它,所以它会打印包含该类的文件的行号。

GetFrame 方法在这里很重要。堆栈帧从 0 开始编号,这是最后推送的堆栈帧。因此,通过引用帧 0,您可以指示运行时打印被推送的最后堆栈帧的行号。当一个方法调用另一个方法时,就会创建一个新的堆栈帧。

相反,您需要在几个重要方面改变您的方法。首先,您需要获取被推入堆栈的第一个帧。其次,您可能想要接受一个包含有关您正在响应的异常的信息的参数。尝试重写您的调试方法,使其看起来像这样:

Public Sub PrintCurrentLine(ByVal ex As Exception)
    Dim st As StackTrace = New StackTrace(ex)
    Dim sf As StackFrame = st.GetFrame(st.FrameCount - 1)
    Console.WriteLine("Line " & sf.GetFileLineNumber())
End Sub

另请记住,如果您在启用优化的情况下运行代码,则行号等内容可能会发生变化。您始终需要在代码中包含 PDB 文件,其中包含在此类情况下使用的调试信息。它将优化后的代码映射回您的原始源代码。

The code you've shown is working exactly as expected. It's printing the number of the line where you captured the stack frame. Because you've defined it in a different class, it's printing the line number of the file that contains that class.

The GetFrame method is important here. Stack frames are numbered starting at 0, which is the last stack frame pushed. So, by referring to frame 0, you are instructing the runtime to print the line number of the last stack frame that was pushed. When one method calls another, a new stack frame is created.

Instead, you need to change your method in a couple of important ways. First, you need to get the first frame that was pushed onto the stack. And second, you probably want to accept a parameter containing information about the exception that you are responding to. Try rewriting your debug method to look something like this:

Public Sub PrintCurrentLine(ByVal ex As Exception)
    Dim st As StackTrace = New StackTrace(ex)
    Dim sf As StackFrame = st.GetFrame(st.FrameCount - 1)
    Console.WriteLine("Line " & sf.GetFileLineNumber())
End Sub

Also remember that if you're running the code with optimizations enabled, things like line numbers may have changed. You always need to include the PDB file with your code, which contains debugging information that is used in situations like this. It maps the optimized code back to your original source.

花开浅夏 2024-11-10 20:12:54

阅读了几个答案后,我找到了以下解决方案,相当于 C++ 宏 LINE

(New StackTrace(New StackFrame(True))).GetFrame(0).GetFileLineNumber())

它可以用作以下示例:

Console.WriteLine(String.Format("Executed on line# {0}", (New StackTrace(New StackFrame(True))).GetFrame(0).GetFileLineNumber()))

After reading the several answers I came for me to the following solution as equivalent to the C++ macro LINE

(New StackTrace(New StackFrame(True))).GetFrame(0).GetFileLineNumber())

which can be used for example as:

Console.WriteLine(String.Format("Executed on line# {0}", (New StackTrace(New StackFrame(True))).GetFrame(0).GetFileLineNumber()))
素衣风尘叹 2024-11-10 20:12:54

编译后的程序集不会有与之关联的行号。这不是程序集一部分的信息。

该信息保存在调试符号文件 - pdb 文件中。

来自 MSDN - StackTrace 类

StackTrace 信息对于调试构建配置来说信息最为丰富。默认情况下,调试版本包含调试符号,而发布版本则不包含。调试符号包含构造 StackFrame 和 StackTrace 对象时使用的大部分文件、方法名称、行号和列信息。

The compiled assemblies will not have line numbers associated with them. This is not information that is part of the assemblies.

The information is kept in the debugging symbols file - the pdb file.

From MSDN - StackTrace class:

StackTrace information will be most informative with Debug build configurations. By default, Debug builds include debug symbols, while Release builds do not. The debug symbols contain most of the file, method name, line number, and column information used in constructing StackFrame and StackTrace objects.

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