如何获取在.NET中抛出的异常的StackTrace中的行号显示 [英] How to get line number(s) in the StackTrace of an exception thrown in .NET to show up

查看:2594
本文介绍了如何获取在.NET中抛出的异常的StackTrace中的行号显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MSDN表示关于 Exception


StackTrace属性保存一个栈
跟踪,您可以使用它来确定
在代码中发生错误。
StackTrace列出了异常
之前的所有
方法,以及源
中的进行调用的行号。

The StackTrace property holds a stack trace, which you can use to determine where in the code the error occurred. StackTrace lists all the called methods that preceded the exception and the line numbers in the source where the calls were made.

所以我知道这些信息是可用的。如何让行号真正显示在堆栈跟踪中?我的代码是在一个非常困难和复杂的代码中抛出一个异常,通过对象的TONS,所以我不想跨越一百亿次,看看异常发生的位置。

So I know that this information is available. How do I get the line numbers to actually show up in the stack trace? My code is throwing an exception in a very difficult and complex piece of code that goes through TONS of objects, so I don't want to step through a bazillion times to see where the exception is happening. The stack trace of the exception only shows method signatures and no line numbers.

推荐答案

要获取StackTrace中的行号,您可以需要在您的dll / exes旁边拥有正确的调试信息(PDB文件)。要生成调试信息,请在项目属性 - >中设置选项。构建 - >高级 - >调试信息

To get the line numbers in the StackTrace, you need to have the correct debug information (PDB files) alongside your dlls/exes. To generate the the debug information, set the option in Project Properties -> Build -> Advanced -> Debug Info:

将其设置为 full 应该足够(请参阅高级构建设置对话框文档,其他选项的作用)。默认情况下,为Debug构建配置生成调试信息(即PDB文件),但也可以为版本构建配置生成调试信息(即PDB文件)。

Setting it to full should suffice (see the Advanced Build Settings Dialog Box docs for what the other options do). Debug info (ie. PDB files) are generated for Debug build configurations by default, but can also be generated for Release build configurations.

生成用于发布版本的PDB使您能够在没有PDB的情况下发送代码,但是如果您需要行号(甚至附加一个),则将PDB放在dll旁边远程调试器)。需要注意的一点是,在版本构建中,由于编译器或JIT编译器进行的优化,行号可能不是完全正确的(如果行号显示为0,则这一点尤为如此)。

Generating PDBs for release builds enables you to ship you code without the PDBs, but to drop the PDBs next to the dlls if you need line numbers (or even to attach a remote debugger). One thing to note is that in a release build, the line numbers may not be entirely correct due to optimisations made by the compiler or the JIT compiler (this is especially so if the line numbers show as 0).

这篇关于如何获取在.NET中抛出的异常的StackTrace中的行号显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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