StackTrace 클래스

정의

여러 스택 프레임의 정렬된 컬렉션에 해당하는 스택 추적을 나타냅니다.

public ref class StackTrace sealed
public ref class StackTrace
public sealed class StackTrace
public class StackTrace
[System.Serializable]
public class StackTrace
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
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.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 정보는 디버그 빌드 구성에 가장 유익합니다. 기본적으로 디버그 빌드에는 디버그 기호가 포함되지만 릴리스 빌드는 그렇지 않습니다. 디버그 기호에는 및 개체 생성 StackFrameStackTrace 에 사용되는 대부분의 파일, 메서드 이름, 줄 번호 및 열 정보가 포함됩니다.

StackTrace 최적화 중에 발생하는 코드 변환으로 인해 메서드 호출이 예상대로 많이 보고되지 않을 수 있습니다.

생성자

StackTrace()

호출자 프레임에서 StackTrace 클래스의 새 인스턴스를 초기화합니다.

StackTrace(Boolean)

필요에 따라 소스 정보를 캡처하고 호출 프레임에서 StackTrace 클래스의 새 인스턴스를 초기화합니다.

StackTrace(Exception)

제공된 예외 개체를 사용하여 StackTrace 클래스의 새 인스턴스를 초기화합니다.

StackTrace(Exception, Boolean)

필요에 따라 소스 정보를 캡처하고 제공된 예외 개체를 사용하여 StackTrace 클래스의 새 인스턴스를 초기화합니다.

StackTrace(Exception, Int32)

제공된 예외 개체를 사용하고 지정된 수의 프레임을 건너뛰어 StackTrace 클래스의 새 인스턴스를 초기화합니다.

StackTrace(Exception, Int32, Boolean)

지정된 수의 프레임을 건너뛰고 필요에 따라 소스 정보를 캡처하여 제공된 예외 개체로 StackTrace 클래스의 새 인스턴스를 초기화합니다.

StackTrace(IEnumerable<StackFrame>)

개체 집합 StackFrame 에서 스택 추적을 생성합니다.

StackTrace(Int32)

지정된 수의 프레임을 건너뛰어 호출자 프레임에서 StackTrace 클래스의 새 인스턴스를 초기화합니다.

StackTrace(Int32, Boolean)

지정된 수의 프레임을 건너뛰고 필요에 따라 소스 정보를 캡처하여 호출자 프레임에서 StackTrace 클래스의 새 인스턴스를 초기화합니다.

StackTrace(StackFrame)

단일 프레임을 포함하는 StackTrace 클래스의 새 인스턴스를 초기화합니다.

StackTrace(Thread, Boolean)
사용되지 않음.

필요에 따라 소스 정보를 캡처하고 특정 스레드에 대해 StackTrace 클래스의 새 인스턴스를 초기화합니다.

이 생성자 오버로드를 사용하지 마십시오.

필드

METHODS_TO_SKIP

스택 추적에서 생략할 기본 메서드 수를 정의합니다. 이 필드는 상수입니다.

속성

FrameCount

스택 추적의 프레임 수를 가져옵니다.

메서드

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
GetFrame(Int32)

지정된 스택 프레임을 가져옵니다.

GetFrames()

현재 스택 추적에 있는 모든 스택 프레임의 복사본을 반환합니다.

GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
ToString()

읽을 수 있도록 스택 추적을 만듭니다.

적용 대상

추가 정보