Exception.StackTrace 属性

定义

获取调用堆栈上的即时框架字符串表示形式。

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

属性值

用于描述调用堆栈的直接帧的字符串。 如果在从语句) 展开堆栈之前等 (没有可用的堆栈 throw 跟踪,则值为 null

实现

示例

下面的代码示例引发 , Exception 然后捕获它并使用 属性显示堆栈跟踪 StackTrace

C#
// 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 返回调用堆栈的帧,这些帧源自引发异常的位置。 可以通过创建 类的新实例 System.Diagnostics.StackTrace 并使用其 StackTrace.ToString 方法获取有关调用堆栈中其他帧的信息。

每当使用 throw 关键字 (keyword) ) (应用程序代码中引发异常时,CLR) 公共语言运行时 (CLR) 更新堆栈跟踪。 如果在与最初引发异常的方法不同的方法中重新引发异常,堆栈跟踪将同时包含最初引发异常的方法中的位置和方法中重新引发异常的位置。 如果在同一方法中引发并随后重新引发异常,则堆栈跟踪仅包含重新引发异常的位置,而不包含最初引发异常的位置。

由于代码转换(如内联)在优化期间发生,属性 StackTrace 可能不会报告尽可能多的方法调用。

继承者说明

属性 StackTrace 在需要控制堆栈跟踪内容或格式的类中被重写。

默认情况下,堆栈跟踪在引发异常对象之前立即捕获。 用于 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
.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

另请参阅