StackTrace 類別

定義

表示堆疊追蹤,即由一個或多個堆疊框架組成的有序集合。

public ref class StackTrace sealed
public ref class StackTrace
public sealed class StackTrace
[System.Serializable]
public class StackTrace
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class StackTrace
public class StackTrace
type StackTrace = class
[<System.Serializable>]
type StackTrace = class
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type StackTrace = class
Public NotInheritable Class StackTrace
Public Class StackTrace
繼承
StackTrace
屬性

範例

以下主控台應用程式示範如何建立一個簡單的 StackTrace 框架,並遍歷其框架以取得除錯與診斷資訊。

using System;
using System.Diagnostics;

class StackTraceSample
{
    [STAThread]
    static void Main(string[] args)
    {
        StackTraceSample sample = new StackTraceSample();
        try
        {
            sample.MyPublicMethod();
        }
        catch (Exception)
        {
            // Create a StackTrace that captures
            // filename, line number, and column
            // information for the current thread.
            StackTrace st = new StackTrace(true);
            for(int i =0; i< st.FrameCount; i++ )
            {
                // Note that high up the call stack, there is only
                // one stack frame.
                StackFrame sf = st.GetFrame(i);
                Console.WriteLine();
                Console.WriteLine("High up the call stack, Method: {0}",
                    sf.GetMethod());

                Console.WriteLine("High up the call stack, Line Number: {0}",
                    sf.GetFileLineNumber());
            }
        }
    }

    public void MyPublicMethod ()
    {
        MyProtectedMethod();
    }

    protected void MyProtectedMethod ()
    {
        MyInternalClass mic = new MyInternalClass();
        mic.ThrowsException();
    }

    class MyInternalClass
    {
        public void ThrowsException()
        {
            try
            {
                throw new Exception("A problem was encountered.");
            }
            catch (Exception e)
            {
                // Create a StackTrace that captures filename,
                // line number and column information.
                StackTrace st = new StackTrace(true);
                string stackIndent = "";
                for(int i =0; i< st.FrameCount; i++ )
                {
                    // Note that at this level, there are four
                    // stack frames, one for each method invocation.
                    StackFrame sf = st.GetFrame(i);
                    Console.WriteLine();
                    Console.WriteLine(stackIndent + " Method: {0}",
                        sf.GetMethod() );
                    Console.WriteLine(  stackIndent + " File: {0}",
                        sf.GetFileName());
                    Console.WriteLine(  stackIndent + " Line Number: {0}",
                        sf.GetFileLineNumber());
                    stackIndent += "  ";
                }
                throw e;
            }
        }
    }
}

/*
This console application produces the following output when
compiled with the Debug configuration.

   Method: Void ThrowsException()
   File: c:\samples\stacktraceframe\myclass.cs
   Line Number: 59

     Method: Void MyProtectedMethod()
     File: c:\samples\stacktraceframe\myclass.cs
     Line Number: 45

       Method: Void MyPublicMethod()
       File: c:\samples\stacktraceframe\myclass.cs
       Line Number: 39

         Method: Void Main(System.String[])
         File: c:\samples\stacktraceframe\myclass.cs
         Line Number: 13

  High up the call stack, Method: Void Main(System.String[])
  High up the call stack, Line Number: 20


This console application produces the following output when
compiled with the Release configuration.

   Method: Void ThrowsException()
   File:
   Line Number: 0

     Method: Void Main(System.String[])
     File:
     Line Number: 0

  High up the call stack, Method: Void Main(System.String[])
  High up the call stack, Line Number: 0

*/
Imports System.Diagnostics

Class StackTraceSample
   
    <STAThread()>  _
    Public Shared Sub Main()

        Dim sample As New StackTraceSample()
        Try
   
            sample.MyPublicMethod()
        Catch
            ' Create a StackTrace that captures
            ' filename, line number, and column
            ' information for the current thread.
            Dim st As New StackTrace(True)
            Dim i As Integer

            For i = 0 To st.FrameCount - 1

                ' Note that high up the call stack, there is only
                ' one stack frame.
                Dim sf As StackFrame = st.GetFrame(i)
                Console.WriteLine()
                Console.WriteLine("High up the call stack, Method: {0}", _
                    sf.GetMethod())
            
                Console.WriteLine("High up the call stack, Line Number: {0}", _
                    sf.GetFileLineNumber())
            Next i
        End Try
    End Sub
   
   Public Sub MyPublicMethod()
      MyProtectedMethod()
   End Sub
    
   Protected Sub MyProtectedMethod()
      Dim mic As New MyInternalClass()
      mic.ThrowsException()
   End Sub
   
   
   Class MyInternalClass
      
      Public Sub ThrowsException()
         Try
            Throw New Exception("A problem was encountered.")
         Catch e As Exception

            ' Create a StackTrace that captures filename,
            ' line number and column information.
            Dim st As New StackTrace(True)

            Dim stackIndent As String = ""
            Dim i As Integer
            For i = 0 To st.FrameCount - 1
               ' Note that at this level, there are four
               ' stack frames, one for each method invocation.
               Dim sf As StackFrame = st.GetFrame(i)
               Console.WriteLine()
               Console.WriteLine(stackIndent + " Method: {0}", _
                   sf.GetMethod())
               Console.WriteLine(stackIndent + " File: {0}", _
                   sf.GetFileName())
               Console.WriteLine(stackIndent + " Line Number: {0}", _
                   sf.GetFileLineNumber())
               stackIndent += "  "
            Next i
            Throw e
         End Try
      End Sub
   End Class
End Class


' This console application produces the following output when
' compiled with the Debug configuration.
'   
'    Method: Void ThrowsException()
'    File: c:\pp\samples\stacktraceframe\myclass.vb
'    Line Number: 55
' 
'      Method: Void MyProtectedMethod()
'      File: c:\pp\samples\stacktraceframe\myclass.vb
'      Line Number: 42
' 
'        Method: Void MyPublicMethod()
'        File: c:\pp\samples\stacktraceframe\myclass.vb
'        Line Number: 37
' 
'          Method: Void Main(System.String[])
'          File: c:\pp\samples\stacktraceframe\myclass.vb
'          Line Number: 13
' 
'   High up the call stack, Method: Void Main(System.String[])
'   High up the call stack, Line Number: 18
' 
' 
' This console application produces the following output when
' compiled with the Release configuration.
' 
'    Method: Void ThrowsException()
'    File:
'    Line Number: 0
' 
'      Method: Void Main(System.String[])
'      File:
'      Line Number: 0
' 
'   High up the call stack, Method: Void Main()
'   High up the call stack, Line Number: 0
'

備註

StackTrace 除錯建置設定的資訊最為有用。 預設情況下,除錯建置包含除錯符號,而 Release 建置則沒有。 除錯符號包含大部分用於建構 StackFrameStackTrace 物件的檔案、方法名稱、行號與欄位資訊。

StackTrace 由於優化過程中發生的程式碼轉換,可能無法回報預期的方法呼叫數量。

建構函式

名稱 Description
StackTrace()

從呼叫者的框架初始化該 StackTrace 類別的新實例。

StackTrace(Boolean)

從呼叫者的框架初始化該類別的新實例 StackTrace ,並可選擇擷取來源資訊。

StackTrace(Exception, Boolean)

初始化該類別的新實例 StackTrace ,使用提供的例外物件,並可選擇性擷取原始資訊。

StackTrace(Exception, Int32, Boolean)

使用提供的例外物件初始化該類別的新實例 StackTrace ,跳過指定的幀數,並可選擇擷取原始資訊。

StackTrace(Exception, Int32)

使用提供的例外物件初始化該類別的新實例 StackTrace ,並跳過指定的幀數。

StackTrace(Exception)

使用提供的例外物件初始化該 StackTrace 類別的新實例。

StackTrace(Int32, Boolean)

從呼叫者的框架初始化該類別的新實例 StackTrace ,跳過指定的幀數,並可選擇擷取來源資訊。

StackTrace(Int32)

從呼叫者的框架初始化該類別的新實例 StackTrace ,跳過指定的幀數。

StackTrace(StackFrame)

初始化包含單一框架的 StackTrace 類別新實例。

StackTrace(Thread, Boolean)
已淘汰.

初始化特定執行緒的新類別實例 StackTrace ,並可選擇擷取原始碼資訊。

不要使用這個建構器過載。

欄位

名稱 Description
METHODS_TO_SKIP

定義了堆疊追蹤中可省略的方法數量的預設值。 此欄位是常數。

屬性

名稱 Description
FrameCount

取得堆疊追蹤中的幀數。

方法

名稱 Description
Equals(Object)

判斷指定的 物件是否等於目前的物件。

(繼承來源 Object)
GetFrame(Int32)

取得指定的堆疊幀。

GetFrames()

回傳目前堆疊追蹤中所有堆疊框架的副本。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前實例的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

建立可讀的堆疊追蹤表示。

適用於

另請參閱