user1679941
user1679941

Reputation:

Is there a way I can send a stack trace to the console in C# with VS2012

I have C# code that's giving an exception. I have the code within a try - catch and I can see the exception but I am having a problem viewing it in VS2012. Is there a way I can send a stack trace to the console so I can look without having to right click on properties of the exception?

Even better does anyone have any code they use to log out more detailed information to the console?

Upvotes: 2

Views: 143

Answers (2)

Totero
Totero

Reputation: 2574

From any piece of code you can access the stack by creating a stackframe object. Lots of information in there and you don't need an exception to access it.

EG.

var callingMethod = new StackFrame(1).GetMethod().Name;

returns the calling method name.

If you have an exception as Joe Daley suggested use

Console.WriteLine(ex); to print it out.

Upvotes: 3

Oded
Oded

Reputation: 499352

ToString is overridden in the Exception classes and will return the stack trace.

catch(Exception ex)
{
  Console.WriteLine(ex.ToString());
}

Upvotes: 3

Related Questions