Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print the current Stack Trace in .NET without any exception?

I have a regular C# code. I have no exceptions. I want to programmatically log the current stack trace for debugging purpose. Example:

public void executeMethod()  {     logStackTrace();     method(); } 
like image 552
Ricibald Avatar asked Feb 10 '09 09:02

Ricibald


People also ask

How do I print current stack trace?

In order to print the stack trace in Java, we use the java. lang. Throwable. printStackTrace() method.

How do I print a stack trace log file?

To print a stack trace to log you Should declare logger and method info(e. toString()) or log(Level.INFO, e. toString()). Logging is the process of writing log messages during the execution of a program to get error and warning messages as well as info messages.

How do I get full stack trace in Visual Studio?

To open the Call Stack window in Visual Studio, from the Debug menu, choose Windows>Call Stack. To set the local context to a particular row in the stack trace display, select and hold (or double click) the first column of the row.


1 Answers

Have a look at the System.Diagnostics namespace. Lots of goodies in there!

System.Diagnostics.StackTrace t = new System.Diagnostics.StackTrace(); 

This is really good to have a poke around in to learn what's going on under the hood.

I'd recommend that you have a look into logging solutions (Such as NLog, log4net or the Microsoft patterns and practices Enterprise Library) which may achieve your purposes and then some.

like image 123
Spence Avatar answered Oct 23 '22 04:10

Spence