StackTrace 构造函数

定义

初始化 StackTrace 类的新实例。

重载

StackTrace()

用调用方的帧初始化 StackTrace 类的新实例。

StackTrace(Boolean)

用调用方的帧初始化 StackTrace 类的新实例,可以选择捕获源信息。

StackTrace(IEnumerable<StackFrame>)

从一组 StackFrame 对象构造堆栈跟踪。

StackTrace(StackFrame)

初始化包含单个帧的 StackTrace 类的新实例。

StackTrace(Exception)

使用提供的异常对象初始化 StackTrace 类的新实例。

StackTrace(Int32)

从调用方的帧初始化 StackTrace 类的新实例,跳过指定的帧数。

StackTrace(Exception, Int32)

使用提供的异常对象初始化 StackTrace 类的新实例,并跳过指定的帧数。

StackTrace(Int32, Boolean)

从调用方的帧初始化 StackTrace 类的新实例,跳过指定的帧数并可以选择捕获源信息。

StackTrace(Thread, Boolean)
已过时.

初始化特定线程的 StackTrace 类的新实例,可以选择捕获源信息。

不要使用此构造函数重载。

StackTrace(Exception, Int32, Boolean)

使用提供的异常对象初始化 StackTrace 类的新实例,跳过指定的帧数并可以选择捕获源信息。

StackTrace(Exception, Boolean)

使用所提供的异常对象初始化 StackTrace 类的新实例,可以选择捕获源信息。

StackTrace()

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

用调用方的帧初始化 StackTrace 类的新实例。

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

示例

下面的代码示例显示堆栈跟踪中的第一个和最后一个函数调用。

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

注解

StackTrace 使用调用方当前线程创建的,不包含文件名、行号或列信息。

如果需要仅包含有关调用堆栈的摘要方法信息的完整跟踪,请使用此无参数构造函数。

适用于

StackTrace(Boolean)

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

用调用方的帧初始化 StackTrace 类的新实例,可以选择捕获源信息。

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

参数

fNeedFileInfo
Boolean

如果为 true,则捕获文件名、行号和列号;否则为 false

示例

下面的代码示例演示了各种 StackTrace 构造函数方法。

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

注解

StackTrace是使用调用方当前线程创建的。

适用于

StackTrace(IEnumerable<StackFrame>)

Source:
StackTrace.cs
Source:
StackTrace.cs

从一组 StackFrame 对象构造堆栈跟踪。

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))

参数

frames
IEnumerable<StackFrame>

堆栈跟踪中应存在的堆栈帧集。

适用于

StackTrace(StackFrame)

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

初始化包含单个帧的 StackTrace 类的新实例。

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)

参数

frame
StackFrame

StackTrace 对象应包含的帧。

示例

下面的代码示例将堆栈跟踪信息写入事件日志条目。

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)

注解

如果不需要完整堆栈跟踪的开销,请使用此构造函数。

另请参阅

适用于

StackTrace(Exception)

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

使用提供的异常对象初始化 StackTrace 类的新实例。

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

参数

e
Exception

从其构造堆栈跟踪的异常对象。

例外

参数 enull

注解

StackTrace 使用调用方当前线程创建的,不包含文件名、行号或列信息。

生成的堆栈跟踪描述发生异常时的堆栈。

另请参阅

适用于

StackTrace(Int32)

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

从调用方的帧初始化 StackTrace 类的新实例,跳过指定的帧数。

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

参数

skipFrames
Int32

堆栈中的帧数,将从其上开始跟踪。

例外

skipFrames 参数为负。

注解

StackTrace 使用调用方当前线程创建的,不包含文件名、行号或列信息。

如果要跳过的帧数大于或等于创建实例时调用堆栈上的帧总数, StackTrace 则 不包含任何帧。

适用于

StackTrace(Exception, Int32)

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

使用提供的异常对象初始化 StackTrace 类的新实例,并跳过指定的帧数。

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)

参数

e
Exception

从其构造堆栈跟踪的异常对象。

skipFrames
Int32

堆栈中的帧数,将从其上开始跟踪。

例外

参数 enull

skipFrames 参数为负。

注解

不包含 StackTrace 文件名、行号或列信息。

生成的堆栈跟踪描述发生异常时的堆栈。

如果要跳过的帧数大于或等于创建实例时调用堆栈上的帧总数, StackTrace 则 不包含任何帧。

另请参阅

适用于

StackTrace(Int32, Boolean)

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

从调用方的帧初始化 StackTrace 类的新实例,跳过指定的帧数并可以选择捕获源信息。

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)

参数

skipFrames
Int32

堆栈中的帧数,将从其上开始跟踪。

fNeedFileInfo
Boolean

如果为 true,则捕获文件名、行号和列号;否则为 false

例外

skipFrames 参数为负。

示例

下面的代码示例演示了各种 StackTrace 构造函数方法。

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

注解

如果要跳过的帧数大于或等于创建实例时调用堆栈上的帧总数, StackTrace 则 不包含任何帧。

适用于

StackTrace(Thread, Boolean)

注意

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

初始化特定线程的 StackTrace 类的新实例,可以选择捕获源信息。

不要使用此构造函数重载。

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)

参数

targetThread
Thread

请求其堆栈跟踪的线程。

needFileInfo
Boolean

如果为 true,则捕获文件名、行号和列号;否则为 false

属性

例外

targetThread 线程未暂停。

注解

重要

不要使用此构造函数。 它已过时,并且没有推荐的替代项。 在挂起线程时,你无法获知它正在执行的代码,并且很容易发生死锁。 例如,如果在安全权限评估期间,您在线程保持锁定的情况下将其挂起,则 AppDomain 中的其他线程可能被阻止。 如果在线程执行类构造函数时挂起该线程,则会阻止尝试使用该类的其他线程 AppDomain

另请参阅

适用于

StackTrace(Exception, Int32, Boolean)

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

使用提供的异常对象初始化 StackTrace 类的新实例,跳过指定的帧数并可以选择捕获源信息。

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)

参数

e
Exception

从其构造堆栈跟踪的异常对象。

skipFrames
Int32

堆栈中的帧数,将从其上开始跟踪。

fNeedFileInfo
Boolean

如果为 true,则捕获文件名、行号和列号;否则为 false

例外

参数 enull

skipFrames 参数为负。

注解

生成的堆栈跟踪描述发生异常时的堆栈。

如果要跳过的帧数大于或等于创建实例时调用堆栈上的帧总数, StackTrace 则 不包含任何帧。

另请参阅

适用于

StackTrace(Exception, Boolean)

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

使用所提供的异常对象初始化 StackTrace 类的新实例,可以选择捕获源信息。

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)

参数

exceptione
Exception

从其构造堆栈跟踪的异常对象。

needFileInfofNeedFileInfo
Boolean

如果为 true,则捕获文件名、行号和列号;否则为 false

例外

参数 enull

注解

生成的堆栈跟踪描述发生异常时的堆栈。

另请参阅

适用于