Editare

Partajați prin


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.

public:
 StackTrace();
public StackTrace ();
Public Sub New ()

Examples

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

void Level5Method()
{
   try
   {
      ClassLevel6^ nestedClass = gcnew ClassLevel6;
      nestedClass->Level6Method();
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( " Level5Method exception handler" );
      StackTrace^ st = gcnew 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;
   }

}
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;
   }
}
Public Sub Level5Method()
   Try
      Dim nestedClass As New ClassLevel6()
      nestedClass.Level6Method()
   Catch e As Exception
      Console.WriteLine(" Level5Method exception handler")
      
      Dim st As New StackTrace()
      
      ' Display the most recent function call.
      Dim sf As StackFrame = st.GetFrame(0)
      Console.WriteLine()
      Console.WriteLine("  Exception in method: ")
      Console.WriteLine("      {0}", sf.GetMethod())
      
      If st.FrameCount > 1 Then
         ' 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())
      End If
      
      Console.WriteLine()
      Console.WriteLine("   ... throwing exception to next level ...")
      Console.WriteLine("-------------------------------------------------")
      Console.WriteLine()
      Throw e
   End Try
End Sub

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

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.

public:
 StackTrace(bool fNeedFileInfo);
public StackTrace (bool fNeedFileInfo);
new System.Diagnostics.StackTrace : bool -> System.Diagnostics.StackTrace
Public Sub New (fNeedFileInfo As Boolean)

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.

void Level2Method()
{
   try
   {
      ClassLevel3^ nestedClass = gcnew ClassLevel3;
      nestedClass->Level3Method();
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( " Level2Method exception handler" );
      
      // Display the full call stack at this level.
      StackTrace^ st1 = gcnew 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 = gcnew StackTrace( gcnew 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 = gcnew 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;
   }

}
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;
   }
}
Public Sub Level2Method()
   Try
      Dim nestedClass As New ClassLevel3
      nestedClass.Level3Method()
   
   Catch e As Exception
      Console.WriteLine(" Level2Method exception handler")
      
      ' Display the full call stack at this level.
      Dim st1 As 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.
      Dim st2 As 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.
      Dim st3 As 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("-------------------------------------------------")
      Console.WriteLine()
      Throw e
   End Try
End Sub

Remarks

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

Applies to

StackTrace(IEnumerable<StackFrame>)

Source:
StackTrace.cs
Source:
StackTrace.cs

Constructs a stack trace from a set of StackFrame objects.

public:
 StackTrace(System::Collections::Generic::IEnumerable<System::Diagnostics::StackFrame ^> ^ frames);
public StackTrace (System.Collections.Generic.IEnumerable<System.Diagnostics.StackFrame> frames);
new System.Diagnostics.StackTrace : seq<System.Diagnostics.StackFrame> -> System.Diagnostics.StackTrace
Public Sub New (frames As IEnumerable(Of StackFrame))

Parameters

frames
IEnumerable<StackFrame>

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

Applies to

StackTrace(StackFrame)

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

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

public:
 StackTrace(System::Diagnostics::StackFrame ^ frame);
public StackTrace (System.Diagnostics.StackFrame frame);
new System.Diagnostics.StackTrace : System.Diagnostics.StackFrame -> System.Diagnostics.StackTrace
Public Sub New (frame As StackFrame)

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.

StackFrame^ fr = gcnew StackFrame( 1,true );
StackTrace^ st = gcnew StackTrace( fr );
EventLog::WriteEntry( fr->GetMethod()->Name, st->ToString(), EventLogEntryType::Warning );
StackFrame fr = new StackFrame(1,true);
StackTrace st = new StackTrace(fr);
EventLog.WriteEntry(fr.GetMethod().Name,
                    st.ToString(),
                    EventLogEntryType.Warning);
Dim frame As New StackFrame(1, True)
Dim strace As New StackTrace(frame)            

EventLog.WriteEntry(frame.GetMethod().Name, _
                    strace.ToString(), _
                    EventLogEntryType.Warning)

Remarks

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

See also

Applies to

StackTrace(Exception)

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

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

public:
 StackTrace(Exception ^ e);
public StackTrace (Exception e);
new System.Diagnostics.StackTrace : Exception -> System.Diagnostics.StackTrace
Public Sub New (e As Exception)

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

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.

public:
 StackTrace(int skipFrames);
public StackTrace (int skipFrames);
new System.Diagnostics.StackTrace : int -> System.Diagnostics.StackTrace
Public Sub New (skipFrames As Integer)

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

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.

public:
 StackTrace(Exception ^ e, int skipFrames);
public StackTrace (Exception e, int skipFrames);
new System.Diagnostics.StackTrace : Exception * int -> System.Diagnostics.StackTrace
Public Sub New (e As Exception, skipFrames As Integer)

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

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.

public:
 StackTrace(int skipFrames, bool fNeedFileInfo);
public StackTrace (int skipFrames, bool fNeedFileInfo);
new System.Diagnostics.StackTrace : int * bool -> System.Diagnostics.StackTrace
Public Sub New (skipFrames As Integer, fNeedFileInfo As Boolean)

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.

void Level2Method()
{
   try
   {
      ClassLevel3^ nestedClass = gcnew ClassLevel3;
      nestedClass->Level3Method();
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( " Level2Method exception handler" );
      
      // Display the full call stack at this level.
      StackTrace^ st1 = gcnew 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 = gcnew StackTrace( gcnew 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 = gcnew 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;
   }

}
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;
   }
}
Public Sub Level2Method()
   Try
      Dim nestedClass As New ClassLevel3
      nestedClass.Level3Method()
   
   Catch e As Exception
      Console.WriteLine(" Level2Method exception handler")
      
      ' Display the full call stack at this level.
      Dim st1 As 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.
      Dim st2 As 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.
      Dim st3 As 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("-------------------------------------------------")
      Console.WriteLine()
      Throw e
   End Try
End Sub

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

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.

public:
 StackTrace(System::Threading::Thread ^ targetThread, bool needFileInfo);
public StackTrace (System.Threading.Thread targetThread, bool needFileInfo);
[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);
new System.Diagnostics.StackTrace : System.Threading.Thread * bool -> System.Diagnostics.StackTrace
[<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")>]
new System.Diagnostics.StackTrace : System.Threading.Thread * bool -> System.Diagnostics.StackTrace
Public Sub New (targetThread As Thread, needFileInfo As Boolean)

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

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.

public:
 StackTrace(Exception ^ e, int skipFrames, bool fNeedFileInfo);
public StackTrace (Exception e, int skipFrames, bool fNeedFileInfo);
new System.Diagnostics.StackTrace : Exception * int * bool -> System.Diagnostics.StackTrace
Public Sub New (e As Exception, skipFrames As Integer, fNeedFileInfo As Boolean)

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

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.

public:
 StackTrace(Exception ^ exception, bool needFileInfo);
public:
 StackTrace(Exception ^ e, bool fNeedFileInfo);
public StackTrace (Exception exception, bool needFileInfo);
public StackTrace (Exception e, bool fNeedFileInfo);
new System.Diagnostics.StackTrace : Exception * bool -> System.Diagnostics.StackTrace
new System.Diagnostics.StackTrace : Exception * bool -> System.Diagnostics.StackTrace
Public Sub New (exception As Exception, needFileInfo As Boolean)
Public Sub New (e As Exception, fNeedFileInfo As Boolean)

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