共用方式為


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

來源:
StackTrace.cs
來源:
StackTrace.cs
來源:
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)

來源:
StackTrace.cs
來源:
StackTrace.cs
來源:
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>)

來源:
StackTrace.cs
來源:
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)

來源:
StackTrace.cs
來源:
StackTrace.cs
來源:
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)

來源:
StackTrace.cs
來源:
StackTrace.cs
來源:
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

例外狀況物件,從其中建構堆疊追蹤。

例外狀況

e 參數為 null

備註

StackTrace 使用呼叫端的目前線程建立 ,而且不包含檔名、行號或數據行資訊。

產生的堆疊追蹤描述例外狀況時的堆疊。

另請參閱

適用於

StackTrace(Int32)

來源:
StackTrace.cs
來源:
StackTrace.cs
來源:
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)

來源:
StackTrace.cs
來源:
StackTrace.cs
來源:
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

要開始追蹤的堆疊上的框架數。

例外狀況

e 參數為 null

skipFrames 參數為負。

備註

StackTrace不包含檔名、行號或數據行資訊。

產生的堆疊追蹤描述例外狀況時的堆疊。

如果要略過的畫面數大於或等於建立實例時呼叫堆疊上的框架總數,則 StackTrace 不會包含任何畫面格。

另請參閱

適用於

StackTrace(Int32, Boolean)

來源:
StackTrace.cs
來源:
StackTrace.cs
來源:
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)

來源:
StackTrace.cs
來源:
StackTrace.cs
來源:
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

例外狀況

e 參數為 null

skipFrames 參數為負。

備註

產生的堆疊追蹤描述例外狀況時的堆疊。

如果要略過的畫面數大於或等於建立實例時呼叫堆疊上的框架總數,則 StackTrace 不會包含任何畫面格。

另請參閱

適用於

StackTrace(Exception, Boolean)

來源:
StackTrace.cs
來源:
StackTrace.cs
來源:
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

例外狀況

e 參數為 null

備註

產生的堆疊追蹤描述例外狀況時的堆疊。

另請參閱

適用於