Environment.StackTrace Environment.StackTrace Environment.StackTrace Environment.StackTrace Property

定義

取得目前的堆疊追蹤資訊。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

屬性值

含有堆疊追蹤資訊的字串。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 'Main
End Class 'Sample
'
'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 : 行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. FullClassName預留位置、 MethodName、、 MethodParams、和會LineNumber取代為實際值, 且定義如下: FileNameThe placeholders, FullClassName, MethodName, MethodParams, FileName, and LineNumber, are replaced by actual values, and are defined as follows:

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

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

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

FileName
宣告MethodName方法之來源檔案的名稱。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指令的來來源程式代碼。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.

安全性

EnvironmentPermission
以取得受許可權保護之資源的完整存取權。for full access to the resource protected by the permission. 相關聯的列舉:UnrestrictedAssociated enumeration: Unrestricted

適用於