StackTrace Constructors

Definition

Initializes a new instance of the StackTrace class.

Overloads

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(IEnumerable<StackFrame>)

Constructs a stack trace from a set of StackFrame objects.

StackTrace(StackFrame)

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

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(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.

StackTrace(Exception, Boolean)

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

StackTrace()

Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs

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

C#
public StackTrace();

Examples

The following code example displays the first and last function calls in a stack trace.

C#
public void Level5Method()
{
   try
   {
      ClassLevel6 nestedClass = new ClassLevel6();
      nestedClass.Level6Method();
   }
   catch (Exception e)
   {
      Console.WriteLine(" Level5Method exception handler");

      StackTrace st = new StackTrace();

      // Display the most recent function call.
      StackFrame sf = st.GetFrame(0);
      Console.WriteLine();
      Console.WriteLine("  Exception in method: ");
      Console.WriteLine("      {0}", sf.GetMethod());

      if (st.FrameCount >1)
      {
         // Display the highest-level function call
         // in the trace.
         sf = st.GetFrame(st.FrameCount-1);
         Console.WriteLine("  Original function call at top of call stack):");
         Console.WriteLine("      {0}", sf.GetMethod());
      }

      Console.WriteLine();
      Console.WriteLine("   ... throwing exception to next level ...");
      Console.WriteLine("-------------------------------------------------\n");
      throw e;
   }
}

Remarks

The StackTrace is created with the caller's current thread, and does not contain file name, line number, or column information.

Use this parameterless constructor when you want a complete trace with only summary method information about the call stack.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

StackTrace(Boolean)

Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs

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

C#
public StackTrace(bool fNeedFileInfo);

Parameters

fNeedFileInfo
Boolean

true to capture the file name, line number, and column number; otherwise, false.

Examples

The following code example demonstrates various StackTrace constructor methods.

C#
public void Level2Method()
{
   try
   {
      ClassLevel3 nestedClass = new ClassLevel3();
      nestedClass.Level3Method();
   }
   catch (Exception e)
   {
      Console.WriteLine(" Level2Method exception handler");

      // Display the full call stack at this level.
      StackTrace st1 = new StackTrace(true);
      Console.WriteLine(" Stack trace for this level: {0}",
         st1.ToString());

      // Build a stack trace from one frame, skipping the current
      // frame and using the next frame.
      StackTrace st2 = new StackTrace(new StackFrame(1, true));
      Console.WriteLine(" Stack trace built with next level frame: {0}",
         st2.ToString());

      // Build a stack trace skipping the current frame, and
      // including all the other frames.
      StackTrace st3 = new StackTrace(1, true);
      Console.WriteLine(" Stack trace built from the next level up: {0}",
         st3.ToString());

      Console.WriteLine();
      Console.WriteLine("   ... throwing exception to next level ...");
      Console.WriteLine("-------------------------------------------------\n");
      throw e;
   }
}

Remarks

The StackTrace is created with the caller's current thread.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

StackTrace(IEnumerable<StackFrame>)

Source:
StackTrace.cs
Source:
StackTrace.cs

Constructs a stack trace from a set of StackFrame objects.

C#
public StackTrace(System.Collections.Generic.IEnumerable<System.Diagnostics.StackFrame> frames);

Parameters

frames
IEnumerable<StackFrame>

The set of stack frames that should be present in the stack trace.

Applies to

.NET 10 and other versions
Product Versions
.NET 8, 9, 10

StackTrace(StackFrame)

Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs

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

C#
public StackTrace(System.Diagnostics.StackFrame frame);

Parameters

frame
StackFrame

The frame that the StackTrace object should contain.

Examples

The following code example writes stack trace information to an event log entry.

C#
StackFrame fr = new StackFrame(1,true);
StackTrace st = new StackTrace(fr);
EventLog.WriteEntry(fr.GetMethod().Name,
                    st.ToString(),
                    EventLogEntryType.Warning);

Remarks

Use this constructor when you do not want the overhead of a full stack trace.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

StackTrace(Exception)

Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs

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

C#
public StackTrace(Exception e);

Parameters

e
Exception

The exception object from which to construct the stack trace.

Exceptions

The parameter e is null.

Remarks

The StackTrace is created with the caller's current thread, and does not contain file name, line number, or column information.

The resulting stack trace describes the stack at the time of the exception.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

StackTrace(Int32)

Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs

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

C#
public StackTrace(int skipFrames);

Parameters

skipFrames
Int32

The number of frames up the stack from which to start the trace.

Exceptions

The skipFrames parameter is negative.

Remarks

The StackTrace is created with the caller's current thread, and does not contain file name, line number, or column information.

If the number of frames to skip is greater than or equal to the total number of frames on the call stack at the time the instance is created, the StackTrace will contain no frames.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

StackTrace(Exception, Int32)

Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs

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

C#
public StackTrace(Exception e, int skipFrames);

Parameters

e
Exception

The exception object from which to construct the stack trace.

skipFrames
Int32

The number of frames up the stack from which to start the trace.

Exceptions

The parameter e is null.

The skipFrames parameter is negative.

Remarks

The StackTrace does not contain file name, line number, or column information.

The resulting stack trace describes the stack at the time of the exception.

If the number of frames to skip is greater than or equal to the total number of frames on the call stack at the time the instance is created, the StackTrace will contain no frames.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

StackTrace(Int32, Boolean)

Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs

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

C#
public StackTrace(int skipFrames, bool fNeedFileInfo);

Parameters

skipFrames
Int32

The number of frames up the stack from which to start the trace.

fNeedFileInfo
Boolean

true to capture the file name, line number, and column number; otherwise, false.

Exceptions

The skipFrames parameter is negative.

Examples

The following code example demonstrates various StackTrace constructor methods.

C#
public void Level2Method()
{
   try
   {
      ClassLevel3 nestedClass = new ClassLevel3();
      nestedClass.Level3Method();
   }
   catch (Exception e)
   {
      Console.WriteLine(" Level2Method exception handler");

      // Display the full call stack at this level.
      StackTrace st1 = new StackTrace(true);
      Console.WriteLine(" Stack trace for this level: {0}",
         st1.ToString());

      // Build a stack trace from one frame, skipping the current
      // frame and using the next frame.
      StackTrace st2 = new StackTrace(new StackFrame(1, true));
      Console.WriteLine(" Stack trace built with next level frame: {0}",
         st2.ToString());

      // Build a stack trace skipping the current frame, and
      // including all the other frames.
      StackTrace st3 = new StackTrace(1, true);
      Console.WriteLine(" Stack trace built from the next level up: {0}",
         st3.ToString());

      Console.WriteLine();
      Console.WriteLine("   ... throwing exception to next level ...");
      Console.WriteLine("-------------------------------------------------\n");
      throw e;
   }
}

Remarks

If the number of frames to skip is greater than or equal to the total number of frames on the call stack at the time the instance is created, the StackTrace will contain no frames.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

StackTrace(Thread, Boolean)

Caution

This constructor has been deprecated. Please use a constructor that does not require a Thread parameter. http://go.microsoft.com/fwlink/?linkid=14202

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

Do not use this constructor overload.

C#
public StackTrace(System.Threading.Thread targetThread, bool needFileInfo);
C#
[System.Obsolete("This constructor has been deprecated.  Please use a constructor that does not require a Thread parameter.  http://go.microsoft.com/fwlink/?linkid=14202")]
public StackTrace(System.Threading.Thread targetThread, bool needFileInfo);

Parameters

targetThread
Thread

The thread whose stack trace is requested.

needFileInfo
Boolean

true to capture the file name, line number, and column number; otherwise, false.

Attributes

Exceptions

The thread targetThread is not suspended.

Remarks

Important

Do not use this constructor. It is obsolete, and there is no recommended alternative. When you suspend a thread, you have no way of knowing what code it is executing, and deadlocks can occur very easily. For example, if you suspend a thread while it holds locks during a security permission evaluation, other threads in the AppDomain might be blocked. If you suspend a thread while it is executing a class constructor, other threads in the AppDomain that attempt to use that class are blocked.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions (Obsolete)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0 (4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1)

StackTrace(Exception, Int32, Boolean)

Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs

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

C#
public StackTrace(Exception e, int skipFrames, bool fNeedFileInfo);

Parameters

e
Exception

The exception object from which to construct the stack trace.

skipFrames
Int32

The number of frames up the stack from which to start the trace.

fNeedFileInfo
Boolean

true to capture the file name, line number, and column number; otherwise, false.

Exceptions

The parameter e is null.

The skipFrames parameter is negative.

Remarks

The resulting stack trace describes the stack at the time of the exception.

If the number of frames to skip is greater than or equal to the total number of frames on the call stack at the time the instance is created, the StackTrace will contain no frames.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

StackTrace(Exception, Boolean)

Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs

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

C#
public StackTrace(Exception exception, bool needFileInfo);
C#
public StackTrace(Exception e, bool fNeedFileInfo);

Parameters

exceptione
Exception

The exception object from which to construct the stack trace.

needFileInfofNeedFileInfo
Boolean

true to capture the file name, line number, and column number; otherwise, false.

Exceptions

The parameter e is null.

Remarks

The resulting stack trace describes the stack at the time of the exception.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0