System.Environment.StackTrace Property

Gets current stack trace information.

Syntax

public static string StackTrace { get; }

Value

A string containing a description of the methods currently in the call stack. This value can be string.Empty .

Remarks

The Environment.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. However, the Environment.StackTrace property might not report as many method calls as expected due to code transformations that occur during optimization.

Note:

For a hierarchical view of the stack trace information by class, use the System.Diagnostics.StackTrace class.

The Environment.StackTrace property formats the stack trace information for each method call as follows:

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

The literal "at" is preceded by three spaces, and the entire substring starting with "in" is omitted if debug symbols are not available. The placeholders, FullClassName, MethodName, MethodParams, FileName, and LineNumber, are replaced by actual values, and are defined as follows:

FullClassName

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 (","). This information is omitted if MethodName takes no parameters.

FileName

The name of the source file where the MethodName method is declared. This information is omitted if debug symbols are not available.

LineNumber

The number of the line in FileName that contains the source code from MethodName for the instruction that is on the call stack. This information is omitted if debug symbols are not available.

The Environment.NewLine string terminates each line of the stack trace.

Example

The following example gets the Environment.StackTrace property from within a series of nested calls.

C# Example

using System;
public class TestCallStack {
    public void MyMethod1 () {
        MyMethod2();
    }
    public void MyMethod2 () {
        MyMethod3();
    }
    public void MyMethod3 () {
        Console.WriteLine("TestCallStack: {0}",
                          Environment.StackTrace);
    }
    public static void Main() {
        TestCallStack t = new TestCallStack();
        t.MyMethod1();
    }
}

Without debug symbols the output is

TestCallStack: at System.Environment.GetStackTrace(Exception e)
at System.Environment.GetStackTrace(Exception e)
at System.Environment.get_StackTrace()
at TestCallStack.Main()

With debug symbols the output is

TestCallStack: at System.Environment.GetStackTrace(Exception e)
at System.Environment.GetStackTrace(Exception e)
at System.Environment.get_StackTrace()
at TestCallStack.MyMethod3() in c:\ECMAExamples\envstack.cs:line 10
at TestCallStack.MyMethod2() in c:\ECMAExamples\envstack.cs:line 8
at TestCallStack.MyMethod1() in c:\ECMAExamples\envstack.cs:line 5
at TestCallStack.Main() in c:\ECMAExamples\envstack.cs:line 15

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0