C# 在发布模式下显示 .NET 程序集的堆栈跟踪中的行数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/628565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display lines number in Stack Trace for .NET assembly in Release mode
提问by Michael Kniskern
Is there a way to display the lines in the stack trace for the .NET assembly build/deployed in Release mode?
有没有办法显示在发布模式下构建/部署的 .NET 程序集的堆栈跟踪中的行?
UPDATE:
更新:
My application is divided into three class library projects and one ASP.NET "website" project. The error I am trying to track down is in one of the three class library projects. I only deployed the pdb file for the class library project that is generating the "Object reference not set to an instance of an object" error.
我的应用程序分为三个类库项目和一个 ASP.NET“网站”项目。我试图追踪的错误是在三个类库项目之一中。我只为生成“对象引用未设置为对象的实例”错误的类库项目部署了 pdb 文件。
The line numbers are still not showing up in the stack trace. Do I need to deploy the pdb files for all projects to get the line numbers in the stack trace?
行号仍然没有显示在堆栈跟踪中。我是否需要为所有项目部署 pdb 文件以获取堆栈跟踪中的行号?
Working solution
工作解决方案
Deploying the pdb file for each application fixed the line number issue.
为每个应用程序部署 pdb 文件修复了行号问题。
采纳答案by Coxy
- Go into the Properties window for the project where you want to see stack trace line numbers.
- Click on the Build "vertical tab".
- Select "Release" configuration.
Check the DEBUG constant parameter. - Uncheck the "Optimize code" parameter to avoid the occasional trace issue with inlined code (this step is not essential).
- Press the Advanced... button and choose Output -> Debug Info -> pdb-only.
- Deploy the generated .pdb file with the assembly.
- 进入要查看堆栈跟踪行号的项目的“属性”窗口。
- 单击构建“垂直选项卡”。
- 选择“发布”配置。
检查 DEBUG 常量参数。 - 取消选中“优化代码”参数以避免内联代码偶尔出现的跟踪问题(此步骤不是必需的)。
- 按 Advanced... 按钮并选择 Output -> Debug Info -> pdb-only。
- 使用程序集部署生成的 .pdb 文件。
Implemented with the comment below:
用下面的评论实现:
- One other thing to check is in the "Package/Publish Web" section that the "Exclude generated debug symbols" checkbox is also unchecked
- 要检查的另一件事是在“打包/发布 Web”部分中,“排除生成的调试符号”复选框也未选中
回答by Ken Browning
Include debug symbols with your build/deployment package.
在您的构建/部署包中包含调试符号。
回答by Darel
In VS 2008 Express, I found it under Project Properties --> Compile --> Advanced Compile Options.
在 VS 2008 Express 中,我在 Project Properties --> Compile --> Advanced Compile Options 下找到了它。
回答by Darel
My solution
我的解决方案
Copy pdb file in same folder that executable file.
将 pdb 文件复制到可执行文件所在的文件夹中。
now i can view the line number when run the exe file.
现在我可以在运行 exe 文件时查看行号。
this is reason
这就是原因
http://msdn.microsoft.com/en-us/library/ee416588%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/ee416588%28v=vs.85%29.aspx
回答by slolife
I've run into problems in the past where I feel the need to deploy PDB files with a release build in order to track down an error. The reason is, like you said, was that the exception occurred in a method that was very large and I could not accurately pinpoint where it was happening.
过去我遇到过一些问题,我觉得需要使用发布版本部署 PDB 文件以跟踪错误。原因是,就像你说的,异常发生在一个非常大的方法中,我无法准确地指出它发生在哪里。
This might be an indication that the method needs to be refactored into smaller, more granular methods. Not a one size fits all answer, but this approach has served me well in the short term (I've often found the bug during the refactoring) and in the long run.
这可能表明该方法需要重构为更小、更细粒度的方法。不是一刀切的答案,但这种方法在短期内(我经常在重构期间发现错误)和长期对我有用。
Just a thought.
只是一个想法。
回答by user3250653
In VS2012 you need to uncheck "Exclude generated debug symbols" in the Package/Publish Web section of the properties as well.
在 VS2012 中,您还需要在属性的“打包/发布 Web”部分中取消选中“排除生成的调试符号”。
回答by Tim Perry
This works every time. You just need to substring the stack trace message. Real Easy! Also, in vb.net you do need to do the "Show All Files" and include the pdb.
这每次都有效。您只需要对堆栈跟踪消息进行子字符串化。真容易!此外,在 vb.net 中,您确实需要执行“显示所有文件”并包含 pdb。
'Err is the exception passed to this function
Dim lineGrab As String = err.StackTrace.Substring(err.StackTrace.Length - 5)
Dim i As Integer = 0
While i < lineGrab.Length
If (IsNumeric(lineGrab(i))) Then
lineNo.Append(lineGrab(i))
End If
i += 1
End While
'LineNo holds the number as a string
C# version:
C#版本:
string lineGrab = error.StackTrace.Substring(error.StackTrace.Length - 5);
int i = 0;
int value;
while (i < lineGrab.Length)
{
if (int.TryParse(lineGrab[i].ToString(), out value))
{
strLineNo.Append(lineGrab[i]);
}
i++;
}