Display lines number in Stack Trace for .NET assembly in Release mode

C#.NetVisual StudioStack TraceLine Numbers

C# Problem Overview


Is there a way to display the lines in the stack trace for the .NET assembly build/deployed in Release mode?

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.

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?

Working solution

Deploying the pdb file for each application fixed the line number issue.

C# Solutions


Solution 1 - C#

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

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

Solution 2 - C#

In VS2012 you need to uncheck "Exclude generated debug symbols" in the Package/Publish Web section of the properties as well.

Solution 3 - C#

My solution

Copy pdb file in same folder that executable file.

now i can view the line number when run the exe file.

this is reason

http://msdn.microsoft.com/en-us/library/ee416588%28v=vs.85%29.aspx

Solution 4 - C#

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.

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.

Solution 5 - C#

Include debug symbols with your build/deployment package.

Solution 6 - C#

In VS 2008 Express, I found it under Project Properties --> Compile --> Advanced Compile Options.

Solution 7 - C#

In .NET Core you need to turn off 'Optimize code' option for release mode to show the correct line number.

C# Compiler Options that control code generation

Solution 8 - C#

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.

'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:

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++;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMichael KniskernView Question on Stackoverflow
Solution 1 - C#CoxyView Answer on Stackoverflow
Solution 2 - C#user3250653View Answer on Stackoverflow
Solution 3 - C#juan carlosView Answer on Stackoverflow
Solution 4 - C#slolifeView Answer on Stackoverflow
Solution 5 - C#Ken BrowningView Answer on Stackoverflow
Solution 6 - C#DarelView Answer on Stackoverflow
Solution 7 - C#user7435032View Answer on Stackoverflow
Solution 8 - C#Tim PerryView Answer on Stackoverflow