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有关调试生成配置的信息将非常丰富。StackTrace information will be most informative with Debug build configurations. 默认情况下,调试版本包含调试符号,而发布版本则不包含。By default, Debug builds include debug symbols, while Release builds do not. 调试符号包含构造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 的浅表副本。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.

适用于

另请参阅