Tuesday, November 27, 2007

Runtime Stacktrace

.NET provides an interesting set of functionality “Stack trace”. Think you want to know the calling method inside from a method, and you want to service according to calling method. Here are few code segments that can help on that occasion. You can use...

Environment.StackTrace

Here you will get full stack trace information. Again .NET provide you Stack trace as a data Structure. See the following code block...

System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(true);

foreach (System.Diagnostics.StackFrame stackFrame in stackTrace.GetFrames())

Console.WriteLine(string.Format("Called from class {0}, in file {1}, inside method {2}, at line number {3}", stackFrame.GetMethod().DeclaringType.Name, stackFrame.GetFileName(), stackFrame.GetMethod().Name, stackFrame.GetFileLineNumber()));

You can iterate through your necessary information. Funny stuff I guess.

No comments:

Post a Comment

Please, no abusive word, no spam.