StackTrace Class

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Represents a stack trace, which is an ordered collection of one or more stack frames.

Namespace:   System.Diagnostics
Assembly:  mscorlib (in mscorlib.dll)

System.Object
  System.Diagnostics.StackTrace

<SerializableAttribute>
<ComVisibleAttribute(True)>
<SecurityPermissionAttribute(SecurityAction.InheritanceDemand,
	UnmanagedCode := True)>
Public Class StackTrace

NameDescription
System_CAPS_pubmethodStackTrace()

Initializes a new instance of the StackTrace class from the caller's frame.

System_CAPS_pubmethodStackTrace(Boolean)

Initializes a new instance of the StackTrace class from the caller's frame, optionally capturing source information.

System_CAPS_pubmethodStackTrace(Exception)

Initializes a new instance of the StackTrace class using the provided exception object.

System_CAPS_pubmethodStackTrace(Exception, Boolean)

Initializes a new instance of the StackTrace class, using the provided exception object and optionally capturing source information.

System_CAPS_pubmethodStackTrace(Exception, Int32)

Initializes a new instance of the StackTrace class using the provided exception object and skipping the specified number of frames.

System_CAPS_pubmethodStackTrace(Exception, Int32, Boolean)

Initializes a new instance of the StackTrace class using the provided exception object, skipping the specified number of frames and optionally capturing source information.

System_CAPS_pubmethodStackTrace(Int32)

Initializes a new instance of the StackTrace class from the caller's frame, skipping the specified number of frames.

System_CAPS_pubmethodStackTrace(Int32, Boolean)

Initializes a new instance of the StackTrace class from the caller's frame, skipping the specified number of frames and optionally capturing source information.

System_CAPS_pubmethodStackTrace(StackFrame)

Initializes a new instance of the StackTrace class that contains a single frame.

System_CAPS_pubmethodStackTrace(Thread, Boolean)

Obsolete. Initializes a new instance of the StackTrace class for a specific thread, optionally capturing source information.

Do not use this constructor overload.

NameDescription
System_CAPS_pubpropertyFrameCount

Gets the number of frames in the stack trace.

NameDescription
System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetFrame(Int32)

Gets the specified stack frame.

System_CAPS_pubmethodGetFrames()

Returns a copy of all stack frames in the current stack trace.

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethodToString()

Builds a readable representation of the stack trace.(Overrides Object.ToString().)

NameDescription
System_CAPS_pubfieldSystem_CAPS_staticMETHODS_TO_SKIP

Defines the default for the number of methods to omit from the stack trace. This field is constant.

StackTrace information will be most informative with Debug build configurations. By default, Debug builds include debug symbols, while Release builds do not. The debug symbols contain most of the file, method name, line number, and column information used in constructing StackFrame and StackTrace objects.

StackTrace might not report as many method calls as expected, due to code transformations that occur during optimization.

The following console application demonstrates how to create a simple StackTrace and iterate through its frames to obtain debugging and diagnostic information.

Imports System
Imports System.Diagnostics

Class StackTraceSample

    <STAThread()>  _
    Public Shared Sub Main()

        Dim sample As New StackTraceSample()
        Try

            sample.MyPublicMethod()
        Catch
            ' Create a StackTrace that captures
            ' filename, line number, and column
            ' information for the current thread.
            Dim st As New StackTrace(True)
            Dim i As Integer

            For i = 0 To st.FrameCount - 1

                ' Note that high up the call stack, there is only
                ' one stack frame.
                Dim sf As StackFrame = st.GetFrame(i)
                Console.WriteLine()
                Console.WriteLine("High up the call stack, Method: {0}", _
                    sf.GetMethod())

                Console.WriteLine("High up the call stack, Line Number: {0}", _
                    sf.GetFileLineNumber())
            Next i
        End Try
    End Sub

   Public Sub MyPublicMethod()
      MyProtectedMethod()
   End Sub

   Protected Sub MyProtectedMethod()
      Dim mic As New MyInternalClass()
      mic.ThrowsException()
   End Sub


   Class MyInternalClass

      Public Sub ThrowsException()
         Try
            Throw New Exception("A problem was encountered.")
         Catch e As Exception

            ' Create a StackTrace that captures filename,
            ' line number and column information.
            Dim st As New StackTrace(True)

            Dim stackIndent As String = ""
            Dim i As Integer
            For i = 0 To st.FrameCount - 1
               ' Note that at this level, there are four
               ' stack frames, one for each method invocation.
               Dim sf As StackFrame = st.GetFrame(i)
               Console.WriteLine()
               Console.WriteLine(stackIndent + " Method: {0}", _
                   sf.GetMethod())
               Console.WriteLine(stackIndent + " File: {0}", _
                   sf.GetFileName())
               Console.WriteLine(stackIndent + " Line Number: {0}", _
                   sf.GetFileLineNumber())
               stackIndent += "  "
            Next i
            Throw e
         End Try
      End Sub
   End Class
End Class


' This console application produces the following output when
' compiled with the Debug configuration.
'   
'    Method: Void ThrowsException()
'    File: c:\pp\samples\stacktraceframe\myclass.vb
'    Line Number: 55
' 
'      Method: Void MyProtectedMethod()
'      File: c:\pp\samples\stacktraceframe\myclass.vb
'      Line Number: 42
' 
'        Method: Void MyPublicMethod()
'        File: c:\pp\samples\stacktraceframe\myclass.vb
'        Line Number: 37
' 
'          Method: Void Main(System.String[])
'          File: c:\pp\samples\stacktraceframe\myclass.vb
'          Line Number: 13
' 
'   High up the call stack, Method: Void Main(System.String[])
'   High up the call stack, Line Number: 18
' 
' 
' This console application produces the following output when
' compiled with the Release configuration.
' 
'    Method: Void ThrowsException()
'    File:
'    Line Number: 0
' 
'      Method: Void Main(System.String[])
'      File:
'      Line Number: 0
' 
'   High up the call stack, Method: Void Main()
'   High up the call stack, Line Number: 0
'

InheritanceDemand

for the ability of inheritors to access unmanaged code. Associated enumeration: SecurityPermissionFlag.UnmanagedCode. This class cannot be inherited by partially trusted code.

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show: