如何获取 .NET 中抛出的异常的 StackTrace 中的行号以显示

发布于 2024-09-25 00:47:28 字数 331 浏览 3 评论 0原文

MSDN 关于 Exception 类的 StackTrace 属性是这样说的:

StackTrace 属性保存一个堆栈 跟踪,您可以用它来确定 代码中哪里发生了错误。 StackTrace列出了所有被调用的 发生异常之前的方法 以及源代码中的行号 拨打电话的地方。

所以我知道这些信息是可用的。如何让行号实际显示在堆栈跟踪中?我的代码在一段非常困难且复杂的代码中抛出异常,该代码遍历大量对象,因此我不想单步执行无数次来查看异常发生的位置。异常的堆栈跟踪仅显示方法签名,不显示行号。

MSDN says this about the StackTrace property of the Exception class:

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.

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.

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

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

发布评论

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

评论(6

萌吟 2024-10-02 00:47:28

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

alt text

将其设置为 full 就足够了(请参阅< a href="http://msdn.microsoft.com/en-us/library/s4wcexbc.aspx" rel="noreferrer">高级构建设置对话框文档了解其他选项的作用)。默认情况下会为调试构建配置生成调试信息(即 PDB 文件),但也可以为发布构建配置生成。

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

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:

alt text

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.

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).

沙沙粒小 2024-10-02 00:47:28

如果您有使用 VS2012 或更高版本的 Web 应用程序或 Web 服务项目,则更改构建设置将不起作用。相反,您应该遵循本文中的建议:

Visual Studio 2012网站发布而不是复制 .pdb 文件

中包含以下设置:

<YOUR_PROJECT>\Properties\PublishProfiles\*.pubxml

具体来说,您应该在项目的文件

<PropertyGroup>
  <ExcludeGeneratedDebugSymbol>False</ExcludeGeneratedDebugSymbol>
</PropertyGroup>

If you have a web application or web service project using VS2012 or later, changing the build settings will not work. Instead, you should follow the advice in this article:

Visual Studio 2012 Website Publish Not Copying .pdb files

Specifically, you should include the following setting in the

<YOUR_PROJECT>\Properties\PublishProfiles\*.pubxml

file(s) for your project:

<PropertyGroup>
  <ExcludeGeneratedDebugSymbol>False</ExcludeGeneratedDebugSymbol>
</PropertyGroup>
樱娆 2024-10-02 00:47:28

如果程序集有一个 pdb 文件,您可以尝试以下操作:

try
{
    throw new Exception();
}
catch (Exception ex)
{
    // Get line number from the stack trace's top frame for the exception with source file information
    int linenumber = (new StackTrace(ex, true)).GetFrame(0).GetFileLineNumber();
}

You could try the following, given there is a pdb file for the assembly:

try
{
    throw new Exception();
}
catch (Exception ex)
{
    // Get line number from the stack trace's top frame for the exception with source file information
    int linenumber = (new StackTrace(ex, true)).GetFrame(0).GetFileLineNumber();
}
病女 2024-10-02 00:47:28

您必须在启用 pdb 文件的情况下构建项目,并确保使用应用程序部署 pdb 文件。您可以通过右键单击需要 pdb 文件的程序集,然后转到“属性”>“属性”来检查 pdb 文件实际上是否正在为您的配置构建。构建>高级并确保输出调试信息下设置为完整。

You have to build the project with pdb files enabled and make sure your deploy the pdb files with your application. You can check if pdb files are in fact being built for you configuration by right clicking on the assembly you require pdb files for, then heading to Properties > Build > Advanced and making sure that under Output Debug Info is set to full.

感悟人生的甜 2024-10-02 00:47:28

除了其他很好的建议之外,我们还部署到 IIS。我们有一个临时服务器和一个生产服务器。它们看起来是一样的,除了舞台给我们提供了生产线编号,而生产却没有。结果发现,生产的 bin 目录中有一个额外的 DLL(它恰好是 SqlServerSpatial.dll fwiw),一旦将其移至系统目录,行号就开始出现在生产中。

教训是确保生产的 bin 目录在各个方面都与开发的 bin 目录匹配(XML 文件除外)。

Further to the other great suggestions, we were deploying to IIS. We had a staging server and a production server. They appeared identical except staging gave us line numbers and production did not. It turned out that there was an extra DLL in the bin directory of production (it happened to be SqlServerSpatial.dll fwiw) and once it was moved to the system directory line numbers started appearing on production.

The lesson was to ensure that the bin directory of production matches the bin directory of development in every respect (except for XML files).

梦屿孤独相伴 2024-10-02 00:47:28

似乎找到了解决办法。
至少在 VS2010 上,将输出调试信息设置为完整时,我也没有在异常中获得行号。技巧似乎是在编辑器中打开行号。 (工具 -> 选项 -> 文本编辑器 -> 所有语言 -> 常规 -> 显示 -> 行号)

现在例外情况会显示行号。

Seem to have found the solution.
On VS2010 at least, with Output Debug Info set to full I also did not get line numbers within the exceptions. The trick it seems was to turn on line numbers within the editor. (Tools -> Options -> Text Editor -> All Languages -> General -> Display -> Line numbers)

Now exceptions show up with line numbers.

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