views:

898

answers:

2

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

public void executeMethod() 
{
    logStackTrace();
    method();
}
+13  A: 

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 whats 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. Good luck mate!

Spence
+7  A: 

There are two ways to do this. The System.Diagnostics.StackTrace() will give you a stack trace for the current thread. If you have a reference to a Thread instance, you can get the stack trace for that via the overloaded version of StackTrace().

You may also want to check out this question.

Brian Rasmussen
Pulling a thread's stacktrace would actually be useful.
Spence