Represents a stack trace, which is an ordered collection of one or more stack frames.
For a list of all members of this type, see StackTrace Members.
System.Object
System.Diagnostics.StackTrace
[Visual Basic] <Serializable> Public Class StackTrace [C#] [Serializable] public class StackTrace [C++] [Serializable] public __gc class StackTrace [JScript] public Serializable class StackTrace
Any public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Any instance members are not guaranteed to be thread safe.
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.
[Visual Basic, C#, C++] The following console application demonstrates how to create a simple StackTrace and iterate through its frames to obtain debugging and diagnostic information.
[Visual Basic] Imports System Imports System.Diagnostics Public Class StackTraceSample Public Shared Sub Main() Try Dim sts As New StackTraceSample() sts.MyPublicSub() Catch ' Create a StackTrace that does not capture filename, line number or column information. Dim strace As New StackTrace() Dim count As Integer ' High up the call stack, there is only one stack frame While count < strace.FrameCount Dim frame As New StackFrame() frame = strace.GetFrame(count) Console.WriteLine("High Up the Call Stack, Method: {0}", frame.GetMethod()) Console.WriteLine("High Up the Call Stack, Line Number: {0}", frame.GetFileLineNumber()) count = count + 1 End While End Try End Sub Public Shared Sub MyPublicSub() MyProtectedSub() End Sub Protected Shared Sub MyProtectedSub() Dim stsh As New StackTraceSampleHelper() stsh.ThrowsException() End Sub End Class Class StackTraceSampleHelper Sub ThrowsException() Try Throw New Exception("A problem was encountered.") Catch e As Exception ' Create a StackTrace that captures filename, line number or column information. Dim strace As New StackTrace(True) Dim count As Integer Dim indent As String = "" ' Low down the call stack, there are four stack frames While count < strace.FrameCount Dim frame As New StackFrame() frame = strace.GetFrame(count) Console.WriteLine(indent & "Method: {0}", frame.GetMethod()) Console.WriteLine(indent & "Line Number: {0}", frame.GetFileLineNumber()) indent = indent & " " count = count + 1 End While Throw e End Try End Sub End Class ' This console application produces the following output when ' compiled with the Debug Configuration. ' ' C:\StackTraceSample\StackTraceSampleVB\bin>StackTraceSampleVB ' Method: Void ThrowsException() ' Line Number: 46 ' Method: Void MyProtectedSub() ' Line Number: 35 ' Method: Void MyPublicSub() ' Line Number: 30 ' Method: Void Main() ' Line Number: 10 ' High Up the Call Stack, Method: Void Main() ' High Up the Call Stack, Line Number: 0 ' ' C:\StackTraceSample\StackTraceSampleVB\bin>StackTraceSampleVB [C#] using System; using System.Diagnostics; class MyClass { [STAThread] static void Main(string[] args) { MyClass mc = new MyClass(); try { mc.MyPublicMethod(); } catch (Exception) { // Create a StackTrace that captures // filename, line number and column // information, for the current thread. StackTrace st = new StackTrace(true); for(int i =0; i< st.FrameCount; i++ ) { // High up the call stack, there is only one stack frame StackFrame sf = st.GetFrame(i); Console.WriteLine("\nHigh Up the Call Stack, Method: {0}", sf.GetMethod() ); Console.WriteLine( "High Up the Call Stack, Line Number: {0}", sf.GetFileLineNumber()); } } } public void MyPublicMethod () { MyProtectedMethod(); } protected void MyProtectedMethod () { MyInternalClass mic = new MyInternalClass(); mic.ThrowsException(); } class MyInternalClass { public void ThrowsException() { try { throw new Exception("A problem was encountered."); } catch (Exception e) { // Create a StackTrace that captures filename, // line number and column information. StackTrace st = new StackTrace(true); string stackIndent = ""; for(int i =0; i< st.FrameCount; i++ ) { // Low down the call stack there are four // stack frames, one for each method invocation. StackFrame sf = st.GetFrame(i); Console.WriteLine("\n" + stackIndent + " Method: {0}", sf.GetMethod() ); Console.WriteLine( stackIndent + " File: {0}", sf.GetFileName()); Console.WriteLine( stackIndent + " Line Number: {0}", sf.GetFileLineNumber()); stackIndent += " "; } throw e; } } } } /* This console application produces the following output when compiled with the Debug Configuration. C:\StackTraceSample\bin\Debug>StackTraceSample Method: Void ThrowsException() File: c:\pp\samples\stacktraceframe\myclass.cs Line Number: 50 Method: Void MyProtectedMethod() File: c:\pp\samples\stacktraceframe\myclass.cs Line Number: 37 Method: Void MyPublicMethod() File: c:\pp\samples\stacktraceframe\myclass.cs Line Number: 31 Method: Void Main(System.String[]) File: c:\pp\samples\stacktraceframe\myclass.cs Line Number: 12 High Up the Call Stack Method: Void Main(System.String[]) High Up the Call Stack Line Number: 17 C:\StackTraceSample\bin\Debug> This console application produces the following output when compiled with the Release Configuration. C:\StackTraceSample\bin\Debug>StackTraceSample Method: Void ThrowsException() File: Line Number: 0 Method: Void Main(System.String[]) File: Line Number: 0 High Up the Call Stack Method: Void Main(System.String[]) High Up the Call Stack Line Number: 0 C:\StackTraceSample\bin\Debug>StackTraceSample */ [C++] #using <mscorlib.dll> #using <System.dll> using namespace System; using namespace System::Diagnostics; __gc class MyClass { private: __gc class MyInternalClass { public: void ThrowsException() { try { throw new Exception(S"A problem was encountered."); } catch (Exception* e) { // Create a StackTrace that captures filename, // line number and column information. StackTrace* st = new StackTrace(true); String* stackIndent = S""; for (int i =0; i< st->FrameCount; i++) { // Low down the call stack there are four // stack frames, one for each method invocation. StackFrame* sf = st->GetFrame(i); Console::WriteLine(S"\n{0} Method: {1}", stackIndent, sf->GetMethod()); Console::WriteLine(S"{0} File: {1}", stackIndent, sf->GetFileName()); Console::WriteLine(S"{0} Line Number: {1}", stackIndent, __box(sf->GetFileLineNumber())); stackIndent = String::Concat(stackIndent, S" "); } throw e; } } }; protected: void MyProtectedMethod() { MyInternalClass* mic = new MyInternalClass(); mic->ThrowsException(); } public: void MyPublicMethod () { MyProtectedMethod(); } }; void main() { MyClass* mc = new MyClass(); try { mc->MyPublicMethod(); } catch (Exception*) { // Create a StackTrace that captures // filename, line number and column // information, for the current thread. StackTrace* st = new StackTrace(true); for (int i = 0; i < st->FrameCount; i++) { // High up the call stack, there is only one stack frame StackFrame* sf = st->GetFrame(i); Console::WriteLine(S"\nHigh Up the Call Stack, Method: {0}", sf->GetMethod()); Console::WriteLine(S"High Up the Call Stack, Line Number: {0}", __box(sf->GetFileLineNumber())); } } } /* This console application produces the following output when compiled with the Debug Configuration. V:\>StackTraceSample Method: Void ThrowsException() File: z:\source.cpp Line Number: 18 Method: Void MyProtectedMethod() File: z:\source.cpp Line Number: 38 Method: Void MyPublicMethod() File: z:\source.cpp Line Number: 43 Method: Int32 main() File: z:\source.cpp Line Number: 49 High Up the Call Stack, Method: Int32 main() High Up the Call Stack, Line Number: 54 ---------- This console application produces the following output when compiled with the Release Configuration. V:\>StackTraceSample Method: Void ThrowsException() File: Line Number: 0 Method: Int32 main() File: Line Number: 0 Method: UInt32 _mainCRTStartup() File: Line Number: 0 High Up the Call Stack, Method: Int32 main() High Up the Call Stack, Line Number: 0 */
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Namespace: System.Diagnostics
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
Assembly: Mscorlib (in Mscorlib.dll)
StackTrace Members | System.Diagnostics Namespace | Exception.StackTrace | Environment.StackTrace | ServerFault.StackTrace