StackTrace Class
Represents a stack trace, which is an ordered collection of one or more stack frames.
Assembly: mscorlib (in mscorlib.dll)
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 '
- SecurityPermission
for the ability to execute unmanaged code. Security action: InheritanceDemand. Associated enumeration: SecurityPermissionFlag.UnmanagedCode
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.