영어로 읽기

다음을 통해 공유


Exception.StackTrace 속성

정의

호출 스택의 직접 실행 프레임 문자열 표현을 가져옵니다.

public virtual string StackTrace { get; }
public virtual string? StackTrace { get; }

속성 값

호출 스택의 직접 실행 프레임을 설명하는 문자열입니다. 사용할 수 있는 스택 추적이 없는 경우(예: 문에서 throw 스택 해제 이전) 값은 입니다 null.

구현

예제

다음 코드 예제를 Exception throw 한 다음 catch 하 고 사용 하 여 스택 추적을 표시 합니다 StackTrace 속성입니다.

// Example for the Exception.HelpLink, Exception.Source,
// Exception.StackTrace, and Exception.TargetSite properties.
using System;

namespace NDP_UE_CS
{
    // Derive an exception; the constructor sets the HelpLink and
    // Source properties.
    class LogTableOverflowException : Exception
    {
        const string overflowMessage = "The log table has overflowed.";

        public LogTableOverflowException(
            string auxMessage, Exception inner ) :
                base( String.Format( "{0} - {1}",
                    overflowMessage, auxMessage ), inner )
        {
            this.HelpLink = "https://learn.microsoft.com";
            this.Source = "Exception_Class_Samples";
        }
    }

    class LogTable
    {
        public LogTable( int numElements )
        {
            logArea = new string[ numElements ];
            elemInUse = 0;
        }

        protected string[ ] logArea;
        protected int       elemInUse;

        // The AddRecord method throws a derived exception if
        // the array bounds exception is caught.
        public    int       AddRecord( string newRecord )
        {
            try
            {
                logArea[ elemInUse ] = newRecord;
                return elemInUse++;
            }
            catch( Exception e )
            {
                throw new LogTableOverflowException(
                    String.Format( "Record \"{0}\" was not logged.",
                        newRecord ), e );
            }
        }
    }

    class OverflowDemo
    {
        // Create a log table and force an overflow.
        public static void Main()
        {
            LogTable log = new LogTable( 4 );

            Console.WriteLine(
                "This example of \n   Exception.Message, \n" +
                "   Exception.HelpLink, \n   Exception.Source, \n" +
                "   Exception.StackTrace, and \n   Exception." +
                "TargetSite \ngenerates the following output." );

            try
            {
                for( int count = 1; ; count++ )
                {
                    log.AddRecord(
                        String.Format(
                            "Log record number {0}", count ) );
                }
            }
            catch( Exception ex )
            {
                Console.WriteLine( "\nMessage ---\n{0}", ex.Message );
                Console.WriteLine(
                    "\nHelpLink ---\n{0}", ex.HelpLink );
                Console.WriteLine( "\nSource ---\n{0}", ex.Source );
                Console.WriteLine(
                    "\nStackTrace ---\n{0}", ex.StackTrace );
                Console.WriteLine(
                    "\nTargetSite ---\n{0}", ex.TargetSite );
            }
        }
    }
}

/*
This example of
   Exception.Message,
   Exception.HelpLink,
   Exception.Source,
   Exception.StackTrace, and
   Exception.TargetSite
generates the following output.

Message ---
The log table has overflowed. - Record "Log record number 5" was not logged.

HelpLink ---
https://learn.microsoft.com

Source ---
Exception_Class_Samples

StackTrace ---
   at NDP_UE_CS.LogTable.AddRecord(String newRecord)
   at NDP_UE_CS.OverflowDemo.Main()

TargetSite ---
Int32 AddRecord(System.String)
*/

설명

실행 스택은 지정된 순간에 실행 중인 모든 메서드를 추적합니다. 메서드 호출의 추적을 스택 추적이라고 합니다. 스택 추적 목록은 예외가 발생하는 메서드의 줄 번호에 대한 호출 스택을 따르는 방법을 제공합니다.

속성은 StackTrace 예외가 throw된 위치에서 발생하는 호출 스택의 프레임을 반환합니다. 클래스의 새 instance 만들고 해당 StackTrace.ToString 메서드를 사용하여 호출 스택의 추가 프레임에 System.Diagnostics.StackTrace 대한 정보를 얻을 수 있습니다.

애플리케이션 코드에서 예외가 발생 될 때마다 스택 추적을 업데이트 하는 CLR (공용 언어 런타임) (사용 하 여는 throw 키워드). 예외가 원래 throw된 메서드와 다른 메서드에서 다시 throw된 경우 스택 추적에는 예외가 원래 throw된 메서드의 위치와 예외가 다시 throw된 메서드의 위치가 모두 포함됩니다. 예외가 throw되고 나중에 동일한 메서드에서 다시 throw되는 경우 스택 추적에는 예외가 다시 발생한 위치만 포함되며 예외가 원래 throw된 위치는 포함되지 않습니다.

속성은 StackTrace 최적화 중에 발생하는 인라인 처리와 같은 코드 변환으로 인해 예상대로 많은 메서드 호출을 보고하지 않을 수 있습니다.

상속자 참고

StackTrace 속성은 스택 추적 콘텐츠 또는 형식을 제어해야 하는 클래스에서 재정의됩니다.

기본적으로 스택 추적은 예외 개체가 throw되기 직전에 캡처됩니다. 예외가 throw되지 않을 때 를 사용하여 StackTrace 스택 추적 정보를 가져옵니다.

적용 대상

제품 버전
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

추가 정보