StackTrace 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 StackTrace 類別的新執行個體。
多載
| 名稱 | Description |
|---|---|
| StackTrace() |
從呼叫者的框架初始化該 StackTrace 類別的新實例。 |
| StackTrace(Boolean) |
從呼叫者的框架初始化該類別的新實例 StackTrace ,並可選擇擷取來源資訊。 |
| 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 類別的新實例。
public:
StackTrace();
public StackTrace();
Public Sub New ()
範例
以下程式碼範例顯示堆疊追蹤中的第一個與最後一個函式呼叫。
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 ,並可選擇擷取來源資訊。
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 建構子方法。
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(StackFrame)
初始化包含單一框架的 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 = 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 類別的新實例。
public:
StackTrace(Exception ^ e);
public StackTrace(Exception e);
new System.Diagnostics.StackTrace : Exception -> System.Diagnostics.StackTrace
Public Sub New (e As Exception)
參數
例外物件,用來構建堆疊追蹤。
例外狀況
參數 e 為 null。
備註
該 是 StackTrace 用呼叫者目前的執行緒建立的,且不包含檔案名稱、行號或欄位資訊。
所得的堆疊追蹤描述例外發生時的堆疊狀況。
另請參閱
適用於
StackTrace(Int32)
從呼叫者的框架初始化該類別的新實例 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 ,並跳過指定的幀數。
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)
參數
例外物件,用來構建堆疊追蹤。
- skipFrames
- Int32
從堆疊上方開始追蹤的幀數。
例外狀況
參數 e 為 null。
skipFrames參數為負數。
備註
不 StackTrace 包含檔案名稱、行號或欄位資訊。
所得的堆疊追蹤描述例外發生時的堆疊狀況。
若跳過的幀數大於或等於建立實例時呼叫堆疊上的總幀數,則 StackTrace 該實例將不包含任何幀。
另請參閱
適用於
StackTrace(Int32, Boolean)
從呼叫者的框架初始化該類別的新實例 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 建構子方法。
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 並未被暫停。
備註
Important
不要使用這個建構子。 它已經過時,且沒有推薦的替代方案。 當你暫停執行緒時,你無法知道它正在執行什麼程式碼,死鎖很容易發生。 例如,如果你在安全權限評估期間暫停一個執行緒,該執行緒仍持有鎖,其他 AppDomain 執行緒可能會被封鎖。 如果你在執行類別建構子時暫停執行緒,該執行緒中嘗試使用該類別的其他執行緒 AppDomain 就會被阻擋。
另請參閱
適用於
StackTrace(Exception, Int32, Boolean)
使用提供的例外物件初始化該類別的新實例 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)
參數
例外物件,用來構建堆疊追蹤。
- skipFrames
- Int32
從堆疊上方開始追蹤的幀數。
- fNeedFileInfo
- Boolean
true擷取檔名、行號及欄號;否則,。 false
例外狀況
參數 e 為 null。
skipFrames參數為負數。
備註
所得的堆疊追蹤描述例外發生時的堆疊狀況。
若跳過的幀數大於或等於建立實例時呼叫堆疊上的總幀數,則 StackTrace 該實例將不包含任何幀。
另請參閱
適用於
StackTrace(Exception, Boolean)
初始化該類別的新實例 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。
備註
所得的堆疊追蹤描述例外發生時的堆疊狀況。