Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get line number(s) in the StackTrace of an exception thrown in .NET to show up

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.

like image 998
Christopher Perry Avatar asked Sep 24 '10 23:09

Christopher Perry


People also ask

How can I get the line number which threw exception?

Simple way, use the Exception. ToString() function, it will return the line after the exception description. You can also check the program debug database as it contains debug info/logs about the whole application.

How can I get line number in stack trace?

The java. lang. StackTraceElement. getLineNumber() method returns the line number of the source line containing the execution point represented by this stack trace element.

How do you analyze a Stacktrace?

Analyze external stack tracesFrom the main menu, select Code | Analyze Stack Trace or Thread Dump. In the Analyze Stack Trace dialog that opens, paste the external stack trace or thread dump into the Put a stacktrace here: text area. Click OK. The stack trace is displayed in the Run tool window.

What is exception Stacktrace?

A trace of the method calls is called a stack trace. The stack trace listing provides a way to follow the call stack to the line number in the method where the exception occurs. The StackTrace property returns the frames of the call stack that originate at the location where the exception was thrown.


2 Answers

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

like image 169
adrianbanks Avatar answered Oct 05 '22 07:10

adrianbanks


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(); } 
like image 39
chridam Avatar answered Oct 05 '22 07:10

chridam