Mono Class Library: System.Environment Overview | Members

System.Environment.StackTrace Property

Returns a string representation of the state of the call stack.

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

This property is read-only.

Note:

An example of how the string returned by this property might be formatted follows, where one line of information is provided for each method on the call stack:

at FullClassName.MethodName(MethodParms) in FileName:line LineNumber

FullClassName, MethodName, MethodParms, FileName, and LineNumber are defined as follows:

ItemDescription
FullClassName The fully qualified name of the class.
MethodNameThe name of the method.
MethodParmsThe list of parameter type/name pairs. Each pair is separated by a comma (,). This information is omitted if MethodName takes zero parameters.
FileNameThe name of the source file where the MethodName method is declared. This information is omitted if debug symbols are not available.
LineNumberThe 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 literal "at" is preceded by a single space.

The literals "in" and ":line" are omitted if debug symbols are not available.

The method calls are described in reverse chronological order (the most recent method call is described first).

Environment.StackTrace might not report as many method calls as expected, due to code transformations that occur during optimization.

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