Environment.StackTrace 屬性

定義

取得目前的堆疊追蹤資訊。Gets current stack trace information.

public:
 static property System::String ^ StackTrace { System::String ^ get(); };
public static string StackTrace { get; }
member this.StackTrace : string
Public Shared ReadOnly Property StackTrace As String

屬性值

String

含有堆疊追蹤資訊的字串。A string containing stack trace information. 這個值可以是 EmptyThis value can be Empty.

範例

下列範例會示範 StackTrace 屬性。The following example demonstrates the StackTrace property.

// Sample for the Environment::StackTrace property
using namespace System;
int main()
{
   Console::WriteLine();
   Console::WriteLine( "StackTrace: ' {0}'", Environment::StackTrace );
}

/*
This example produces the following results:

StackTrace: '   at System::Environment::GetStackTrace(Exception e)
at System::Environment::GetStackTrace(Exception e)
at System::Environment::get_StackTrace()
at Sample::Main()'
*/
// Sample for the Environment.StackTrace property
using System;

class Sample
{
    public static void Main()
    {
    Console.WriteLine();
    Console.WriteLine("StackTrace: '{0}'", Environment.StackTrace);
    }
}
/*
This example produces the following results:

StackTrace: '   at System.Environment.GetStackTrace(Exception e)
   at System.Environment.GetStackTrace(Exception e)
   at System.Environment.get_StackTrace()
   at Sample.Main()'
*/
' Sample for the Environment.StackTrace property
Class Sample
   Public Shared Sub Main()
      Console.WriteLine()
      Console.WriteLine("StackTrace: '{0}'", Environment.StackTrace)
   End Sub
End Class
'
'This example produces the following results:
'
'StackTrace: '   at System.Environment.GetStackTrace(Exception e)
'   at System.Environment.GetStackTrace(Exception e)
'   at System.Environment.get_StackTrace()
'   at Sample.Main()'
'

備註

StackTrace屬性會以反向時間順序列出方法呼叫,也就是最新的方法呼叫會先說明,並針對堆疊上的每個方法呼叫列出一行堆疊追蹤資訊。The StackTrace property lists method calls in reverse chronological order, that is, the most recent method call is described first, and one line of stack trace information is listed for each method call on the stack. 不過, StackTrace 由於優化期間發生的程式碼轉換,屬性可能不會報告預期的方法呼叫數目。However, the StackTrace property might not report as many method calls as expected due to code transformations that occur during optimization.

注意

如需依類別的堆疊追蹤資訊的階層式流覽,請使用 StackTrace 類別。For a hierarchical view of the stack trace information by class, use the StackTrace class.

StackTrace屬性會格式化每個方法呼叫的堆疊追蹤資訊,如下所示:The StackTrace property formats the stack trace information for each method call as follows:

FullClassNameMethodName (MethodParams) 在 FileName : line LineNumber ""at FullClassName.MethodName(MethodParams) in FileName :line LineNumber "

常值 "at" 前面會有三個空格,如果無法使用 debug 符號,則會省略以 "in" 開頭的整個子字串。The literal "at" is preceded by three spaces, and the entire substring starting with "in" is omitted if debug symbols are not available. 預留位置、 FullClassNameMethodNameMethodParamsFileNameLineNumber 會由實際值取代,而且定義如下:The placeholders, FullClassName, MethodName, MethodParams, FileName, and LineNumber, are replaced by actual values, and are defined as follows:

FullClassName 類別的完整名稱,包括命名空間。FullClassName The full name of the class, including the namespace.

MethodName 方法的名稱。MethodName The name of the method.

MethodParams 參數類型/名稱組的清單。MethodParams The list of parameter type/name pairs. 每一組都會以逗號分隔 ( "," ) 。Each pair is separated by a comma (","). 如果不接受任何參數,則會省略此資訊 MethodNameThis information is omitted if MethodName takes no parameters.

FileName 宣告方法的原始程式檔名稱 MethodNameFileName The name of the source file where the MethodName method is declared. 如果無法使用 debug 符號,則會省略這項資訊。This information is omitted if debug symbols are not available.

LineNumber 中的行號 FileName ,其中包含 MethodName 呼叫堆疊上之指令的原始程式碼。LineNumber The number of the line in FileName that contains the source code from MethodName for the instruction that is on the call stack. 如果無法使用 debug 符號,則會省略這項資訊。This information is omitted if debug symbols are not available.

Environment.NewLine字串會結束堆疊追蹤的每一行。The Environment.NewLine string terminates each line of the stack trace.

適用於