Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

July 28, 2009

Caller Id: Using StackTrace

Filed under: C#,Debugging,VB.NET @ 12:43 pm

There are situations where you may want to know who called a particular method in your application. Maybe you want to log it or use the information for debugging purposes. Whatever the reason, you can get information on the call stack using the StackTrace class.

A stack trace is basically information on the active stack while a program is running. See this for more basic information on stack trace.

The .NET StackTrace class is detailed here. This post provides some examples of using the StackTrace class to obtain information about the call stack.

If you want to view the entire StackTrace, you can loop through each stack frame.

NOTE: Be sure to set a reference to System.Diagnostics.

In C#:

private string MyMethod(string s1)
{

  StackTrace st = new StackTrace();
  foreach (StackFrame sf in st.GetFrames())
  {
    Debug.WriteLine("Calling class name: " +
                                sf.GetMethod().ReflectedType.Name);
    Debug.WriteLine("Calling method name: " + sf.GetMethod().Name);
    Debug.WriteLine("Full method signature: " +
                                        sf.GetMethod().ToString());

  } 
  return String.Empty;
}

In VB:

Public Function MyMethod(ByVal s1 As String) As String
  Dim st As New StackTrace
  For Each sf As StackFrame In st.GetFrames
    Debug.WriteLine("Calling class name: " & _
                                sf.GetMethod.ReflectedType.Name)
    Debug.WriteLine("Calling method name: " & sf.GetMethod.Name)
    Debug.WriteLine("Full method signature: " & sf.GetMethod.ToString)
  Next
  Return String.Empty
End Function

The GetMethod function gets the method executing in the defined stack frame. The 0th stack frame is the currently executing method. The 1st stack frame is the method that called it, the 2nd stack frame is the method that called that method, and so on up the call stack.

The Name property defines the name of the method. The ReflectedType property defines the class of the calling method and the ReflectedType.Name property provides the name of that class.

The ToString method provides the full method signature. Regardless of the language you are using, the signature will always be displayed in C# style syntax.

For example, the first few stack frames from my example application are as follows:

Calling class name: TestWin
Calling method name: MyMethod
Full method signature: System.String MyMethod(System.String)

Calling class name: TestWin
Calling method name: WorkWithMethods
Full method signature: Void WorkWithMethods()

Calling class name: TestWin
Calling method name: TestWin_Load
Full method signature: Void TestWin_Load(System.Object, System.EventArgs)

If you only want information about the direct caller, you can access the first stack frame.

In C#:

StackTrace st = new StackTrace();
StackFrame sfFirst = st.GetFrame(1);
Debug.WriteLine("Calling class name: " +
                            sfFirst.GetMethod().ReflectedType.Name);
Debug.WriteLine("Calling method name: " + sfFirst.GetMethod().Name);

In VB:

Dim st As New StackTrace
Dim sfFirst As StackFrame = st.GetFrame(1)
Debug.WriteLine("Calling class name: " & _
                            sfFirst.GetMethod.ReflectedType.Name)
Debug.WriteLine("Calling method name: " & sfFirst.GetMethod.Name)

This code provides the name of the class and method that directly called the current method:

Calling class name: TestWin
Calling method name: WorkWithMethods

Enjoy!

1 Comment

  1.   Jason — November 20, 2009 @ 2:08 am    Reply

    You have no example with getting line I will go with Charlieit123’s solution! E for Effort!

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2020 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs