154

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.

Michael Kniskern
  • 24,792
  • 68
  • 164
  • 231

8 Answers8

163
  • 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
Community
  • 1
  • 1
Coxy
  • 8,844
  • 4
  • 39
  • 62
  • 2
    Do I have to deploy the pdb file along with the assembly? – Michael Kniskern Mar 10 '09 at 01:10
  • 7
    Yes. That's where the debug symbols and line numbers are at. – John Saunders Mar 10 '09 at 01:21
  • 5
    You probably don't want to expose this information if you don't have to. Use it to debug a clients problem, yes. But you don't always want to do it because debugging information can give away sensitive data and be an attack vector. Depending on what your app is. – i_am_jorf Mar 10 '09 at 02:21
  • Is there any way to do this when installing dlls in the GAC? – Jonathan Parker Mar 20 '09 at 07:11
  • 1
    @coxymla: I tried getting the method name, file name and line number without unchecking "optimize code", and without changing the Debug Info (it was in "full" by default). Do you know why it still works? – Carlo Mar 01 '10 at 20:12
  • @Carlo - not 100% sure from what you've posted. You do or you don't get line numbers? Anyway, I recommend asking a new question for your particular situation. – Coxy Mar 02 '10 at 06:29
  • 6
    @Carlo: Debug information works with release (optimized) code as well, however debugging is somewhat limited (http://stackoverflow.com/questions/113866). However callstacks are quite reliable even in optimized code, with exception of inlined functions and ocasional situations where tail call can be missing because call xxx / ret sequence was replaced with jmp xxx. – Suma Mar 02 '10 at 09:58
  • 1
    @Carlo, it should work with Debug Info = "full", too. It just wouldn't work with it set to "none", I think this is why the answer said to change it to "pdb-only". – jwg Jan 29 '13 at 11:23
  • 12
    One other thing to check is in the "Package/Publish Web" section that the "Exclude generated debug symbols" checkbox is also unchecked – Gaz May 02 '14 at 08:46
  • 1
    @i_am_jorf Can you give a reference to any substantial security info that can leak via pdb-only debug? I've heard this is a myth and I've never seen an example (aside from inserting path/server names but these are easy to make meaningless via mapped drives, etc). – FastAl Nov 23 '16 at 20:23
17

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

Rémi
  • 3,867
  • 5
  • 28
  • 44
17

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

user3250653
  • 171
  • 1
  • 2
11

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.

slolife
  • 19,520
  • 20
  • 78
  • 121
  • This. And as you go, throw try catches in more sketchy places at a finer grain. And increase the guards at the beginning of these functions if assumptions have to be made. – Gerard ONeill Oct 11 '15 at 01:03
  • Often said and so true, however, there is legacy, there are programmers writing new large methods, and sometimes a large method is actually the best thing to do (splitting it is confusing or YAGNI). Plus, even for a 5 line method - you narrow your search 5x - so PDBs are a needed evil in production unless you take the pain of using a symbol server – FastAl Nov 23 '16 at 20:26
4

Include debug symbols with your build/deployment package.

Ken Browning
  • 28,693
  • 6
  • 56
  • 68
0

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

0

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

Darel
  • 640
  • 1
  • 7
  • 26
-6

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++;
}
Randy Levy
  • 22,566
  • 4
  • 68
  • 94