Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get full stack trace with line numbers

Tags:

c#

.net

Is it possible to get a full StackTrace object WITH line numbers at any given point in the code

I found this:

var stackTrace = new StackTrace();

That gives me the full stacktrace from where I am in execution. But it does not include line numbers.

I also found this:

var stackTrace = new StackTrace(new StackFrame(1, true));

That gives me the line numbers, but only for one frame of the StackTrace (not a full StackTrace).

Is there a way to get both together?

NOTE: I don't have an exception I am working with. I am just at a point in the code where I want to log the stack trace in a custom way.

NOTE: I know about Environment.Properties.StackTrace. But that returns a string, not a StackTrace object.

like image 580
Vaccano Avatar asked Feb 27 '15 22:02

Vaccano


1 Answers

I suspect you just want to call the overload taking a bool:

var stackTrace = new StackTrace(true);

From the documentation:

Parameters

fNeedFileInfo
Type: System.Boolean
true to capture the file name, line number, and column number; otherwise, false.

like image 87
Jon Skeet Avatar answered Sep 29 '22 09:09

Jon Skeet