如何在VB.net中运行应用程序期间打印出行号
我想在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您所显示的代码完全按照预期工作。它正在打印捕获堆栈帧的行号。因为您已在不同的类中定义了它,所以它会打印包含该类的文件的行号。
GetFrame 方法在这里很重要。堆栈帧从 0 开始编号,这是最后推送的堆栈帧。因此,通过引用帧 0,您可以指示运行时打印被推送的最后堆栈帧的行号。当一个方法调用另一个方法时,就会创建一个新的堆栈帧。
相反,您需要在几个重要方面改变您的方法。首先,您需要获取被推入堆栈的第一个帧。其次,您可能想要接受一个包含有关您正在响应的异常的信息的参数。尝试重写您的调试方法,使其看起来像这样:
另请记住,如果您在启用优化的情况下运行代码,则行号等内容可能会发生变化。您始终需要在代码中包含 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:
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.
阅读了几个答案后,我找到了以下解决方案,相当于 C++ 宏 LINE,
它可以用作以下示例:
After reading the several answers I came for me to the following solution as equivalent to the C++ macro LINE
which can be used for example as:
编译后的程序集不会有与之关联的行号。这不是程序集一部分的信息。
该信息保存在调试符号文件 -
pdb
文件中。来自 MSDN - 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: