StackTrace Class
Represents a stack trace, which is an ordered collection of one or more stack frames.
Namespace: System.Diagnostics
Assembly: mscorlib (in mscorlib.dll)
The StackTrace type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | StackTrace() | Initializes a new instance of the StackTrace class from the caller's frame. |
![]() | StackTrace(Boolean) | Initializes a new instance of the StackTrace class from the caller's frame, optionally capturing source information. |
![]() | StackTrace(Exception) | Initializes a new instance of the StackTrace class using the provided exception object. |
![]() | StackTrace(Int32) | Initializes a new instance of the StackTrace class from the caller's frame, skipping the specified number of frames. |
![]() | StackTrace(StackFrame) | Initializes a new instance of the StackTrace class that contains a single frame. |
![]() | StackTrace(Exception, Boolean) | Initializes a new instance of the StackTrace class, using the provided exception object and optionally capturing source information. |
![]() | StackTrace(Exception, Int32) | Initializes a new instance of the StackTrace class using the provided exception object and skipping the specified number of frames. |
![]() | StackTrace(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. |
![]() | StackTrace(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. |
![]() | StackTrace(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. |
| Name | Description | |
|---|---|---|
![]() | Equals(Object) | Determines whether the specified object is equal to the current object. (Inherited from Object.) |
![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
![]() | GetFrame | Gets the specified stack frame. |
![]() | GetFrames | Returns a copy of all stack frames in the current stack trace. |
![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | ToString | Builds a readable representation of the stack trace. (Overrides Object.ToString().) |
| Name | Description | |
|---|---|---|
![]() ![]() | METHODS_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.
using System; using System.Diagnostics; class StackTraceSample { [STAThread] static void Main(string[] args) { StackTraceSample sample = new StackTraceSample(); try { sample.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++ ) { // Note that high up the call stack, there is only // one stack frame. StackFrame sf = 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()); } } } 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++ ) { // Note that at this level, there are four // stack frames, one for each method invocation. StackFrame sf = 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 += " "; } throw e; } } } } /* This console application produces the following output when compiled with the Debug configuration. Method: Void ThrowsException() File: c:\samples\stacktraceframe\myclass.cs Line Number: 59 Method: Void MyProtectedMethod() File: c:\samples\stacktraceframe\myclass.cs Line Number: 45 Method: Void MyPublicMethod() File: c:\samples\stacktraceframe\myclass.cs Line Number: 39 Method: Void Main(System.String[]) File: c:\samples\stacktraceframe\myclass.cs Line Number: 13 High up the call stack, Method: Void Main(System.String[]) High up the call stack, Line Number: 20 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(System.String[]) 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.
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.




