StackTrace StackTrace StackTrace StackTrace Class

定義

表示堆疊追蹤,這是一個或多個堆疊框架的已排序集合。Represents a stack trace, which is an ordered collection of one or more stack frames.

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

範例

下列主控台應用程式會示範如何建立簡單StackTrace的並逐一查看其框架,以取得偵錯工具和診斷資訊。The following console application demonstrates how to create a simple StackTrace and iterate through its frames to obtain debugging and diagnostic information.

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
ref class StackTraceSample
{
private:
   ref class MyInternalClass
   {
   public:
      void ThrowsException()
      {
         try
         {
            throw gcnew Exception( "A problem was encountered." );
         }
         catch ( Exception^ e ) 
         {
            
            // Create a StackTrace that captures
            // filename, line number, and column
            // information for the current thread.
            StackTrace^ st = gcnew StackTrace( true );
            String^ stackIndent = "";
            for ( int i = 0; i < st->FrameCount; i++ )
            {
               
               // Note that at this level, there are five
               // stack frames, one for each method invocation.
               StackFrame^ sf = st->GetFrame( i );
               Console::WriteLine();
               Console::WriteLine( "{0}Method: {1}", stackIndent, sf->GetMethod() );
               Console::WriteLine( "{0}File: {1}", stackIndent, sf->GetFileName() );
               Console::WriteLine( "{0}Line Number: {1}", stackIndent, sf->GetFileLineNumber().ToString() );
               stackIndent = String::Concat( stackIndent, "  " );

            }
            throw e;
         }

      }

   };


protected:
   void MyProtectedMethod()
   {
      MyInternalClass^ mic = gcnew MyInternalClass;
      mic->ThrowsException();
   }


public:
   void MyPublicMethod()
   {
      MyProtectedMethod();
   }

};

int main()
{
   StackTraceSample^ sample = gcnew StackTraceSample;
   try
   {
      sample->MyPublicMethod();
   }
   catch ( Exception^ ) 
   {
      
      // Create a StackTrace that captures
      // filename, line number, and column
      // information for the current thread.
      StackTrace^ st = gcnew StackTrace( true );
      for ( int i = 0; i < st->FrameCount; i++ )
      {
         
         // For an executable built from C++, there
         // are two stack frames here: one for main,
         // and one for the _mainCRTStartup stub. 
         StackFrame^ sf = st->GetFrame( i );
         Console::WriteLine();
         Console::WriteLine( "High up the call stack, Method: {0}", sf->GetMethod()->ToString() );
         Console::WriteLine( "High up the call stack, Line Number: {0}", sf->GetFileLineNumber().ToString() );

      }
   }

}

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

  Method: Void ThrowsException()
  File: c:\samples\stacktraceframe\myclass.cpp
  Line Number: 20

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

      Method: Void MyPublicMethod()
      File: c:\samples\stacktraceframe\myclass.cpp
      Line Number: 50
 
        Method: Int32 main()
        File: c:\samples\stacktraceframe\myclass.cpp
        Line Number: 56

          Method: UInt32 _mainCRTStartup()
          File:
          Line Number: 0

  High up the call stack, Method: Int32 main()
  High up the call stack, Line Number: 62

  High up the call stack, Method: UInt32 _mainCRTStartup()
  High up the call stack, Line Number: 0

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

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

    Method: Int32 main()
    File:
    Line Number: 0

      Method: UInt32 _mainCRTStartup()
      File:
      Line Number: 0

  High up the call stack, Method: Int32 main()
  High up the call stack, Line Number: 0

  High up the call stack, Method: UInt32 _mainCRTStartup()
  High up the call stack, Line Number: 0

*/
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資訊會與 Debug 組建設定有關。StackTrace information will be most informative with Debug build configurations. 根據預設,Debug 組建會包含 debug 符號,而發行組建則不會。By default, Debug builds include debug symbols, while Release builds do not. Debug 符號包含用來建立StackFrameStackTrace物件的大部分檔案、方法名稱、行號和欄資訊。The debug symbols contain most of the file, method name, line number, and column information used in constructing StackFrame and StackTrace objects.

StackTrace可能因為優化期間發生程式碼轉換,而無法報告所預期的多個方法呼叫。StackTrace might not report as many method calls as expected, due to code transformations that occur during optimization.

建構函式

StackTrace() StackTrace() StackTrace() StackTrace()

從呼叫端的框架來初始化 StackTrace 類別的新執行個體。Initializes a new instance of the StackTrace class from the caller's frame.

StackTrace(Boolean) StackTrace(Boolean) StackTrace(Boolean) StackTrace(Boolean)

從呼叫端的框架來初始化 StackTrace 類別的新執行個體,可以選擇性地擷取來源資訊。Initializes a new instance of the StackTrace class from the caller's frame, optionally capturing source information.

StackTrace(Exception) StackTrace(Exception) StackTrace(Exception) StackTrace(Exception)

使用提供的例外狀況物件初始化 StackTrace 類別的新執行個體。Initializes a new instance of the StackTrace class using the provided exception object.

StackTrace(Exception, Boolean) StackTrace(Exception, Boolean) StackTrace(Exception, Boolean) StackTrace(Exception, Boolean)

使用提供的例外狀況物件,及選擇性地擷取來源資訊,來初始化 StackTrace 類別的新執行個體。Initializes a new instance of the StackTrace class, using the provided exception object and optionally capturing source information.

StackTrace(Exception, Int32) StackTrace(Exception, Int32) StackTrace(Exception, Int32) StackTrace(Exception, Int32)

使用所提供的例外狀況物件初始化 StackTrace 類別的新執行個體,並跳過指定數量的框架。Initializes a new instance of the StackTrace class using the provided exception object and skipping the specified number of frames.

StackTrace(Exception, Int32, Boolean) StackTrace(Exception, Int32, Boolean) StackTrace(Exception, Int32, Boolean) StackTrace(Exception, Int32, Boolean)

使用所提供的例外狀況物件將 StackTrace 類別的新執行個體初始化,並跳過指定數量的框架,亦可選擇性擷取來源資訊。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(Int32) StackTrace(Int32) StackTrace(Int32) StackTrace(Int32)

從呼叫端的框架初始化 StackTrace 類別的新執行個體,略過指定的框架數。Initializes a new instance of the StackTrace class from the caller's frame, skipping the specified number of frames.

StackTrace(Int32, Boolean) StackTrace(Int32, Boolean) StackTrace(Int32, Boolean) StackTrace(Int32, Boolean)

使用呼叫端的框架初始化 StackTrace 類別的新執行個體,並跳過指定數量的框架,亦可選擇性擷取來源資訊。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(StackFrame) StackTrace(StackFrame) StackTrace(StackFrame) StackTrace(StackFrame)

初始化 StackTrace 類別的新執行個體,這個執行個體含有單一框架。Initializes a new instance of the StackTrace class that contains a single frame.

StackTrace(Thread, Boolean) StackTrace(Thread, Boolean) StackTrace(Thread, Boolean) StackTrace(Thread, Boolean)

為特定執行緒、並選擇性地擷取來源資訊,來初始化 StackTrace 類別的新執行個體。Initializes a new instance of the StackTrace class for a specific thread, optionally capturing source information.

不要使用這個建構函式多載。Do not use this constructor overload.

欄位

METHODS_TO_SKIP METHODS_TO_SKIP METHODS_TO_SKIP METHODS_TO_SKIP

定義要從堆疊追蹤省略的方法數預設值。Defines the default for the number of methods to omit from the stack trace. 這個欄位是常數。This field is constant.

屬性

FrameCount FrameCount FrameCount FrameCount

取得堆疊追蹤中的框架數。Gets the number of frames in the stack trace.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetFrame(Int32) GetFrame(Int32) GetFrame(Int32) GetFrame(Int32)

取得指定的堆疊框架。Gets the specified stack frame.

GetFrames() GetFrames() GetFrames() GetFrames()

傳回目前堆疊追蹤內的所有堆疊框架的複本。Returns a copy of all stack frames in the current stack trace.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

建立堆疊追蹤的可讀取表示。Builds a readable representation of the stack trace.

安全性

InheritanceDemand
讓繼承者能夠存取非受控碼。for the ability of inheritors to access unmanaged code. 關聯的列舉型別:UnmanagedCodeAssociated enumeration: UnmanagedCode. 此類別無法由部分信任程式碼繼承。This class cannot be inherited by partially trusted code.

適用於

另請參閱