Open In App

C# Program to Get Current Stack Trace Information Using Environment Class

In C#, Environment Class provides information about the current platform and manipulates, the current platform. It is useful for getting and setting various operating system-related information. We can use it in such a way that it retrieves command-line arguments information, exit codes information, environment variable settings information, contents of the call stack information, and time since the last system boot in milliseconds information. By just using some predefined methods we can get the information of the Operating System using the Environment class. Here in this article, we are using the StackTrace property to get the current stack trace information. This property is used to get the current stack trace information

Syntax:



string Environment.StackTrace

Return: string and gives the class name along with id 



Example 1:




// C# program to find the current stack trace information
using System;
 
class GFG{
 
static public void Main()
{
     
    // Define the variable with string
    string my_variable = "Hello Geeks";
 
    // Get the current stack trace details
    // Using StackTrace property
    my_variable = Environment.StackTrace;
    Console.WriteLine("Information of current stack trace: \n" +
                      my_variable);
}
}

Output:

Current Stack Trace Details: 
  at System.Environment.get_StackTrace () [0x00000] in <a1ae6166591d4020b810288d19af38d4>:0 
  at GFG.Main () [0x00000] in <ec0c23afce674aedba9f00d218402cf6>:0

Example 2:




// C# program to find the current stack trace information
using System;
 
class GFG{
 
static public void Main()
{
     
    // Define the variable with integer
    string my_variable = "58";
     
    // Get the current stack trace details
    // Using StackTrace property
    my_variable = Environment.StackTrace;
    Console.WriteLine("Information of current stack trace: \n" +
                      my_variable);
}
}

Output:

Current Stack Trace Details: 
  at System.Environment.get_StackTrace () [0x00000] in <a1ae6166591d4020b810288d19af38d4>:0 
  at GFG.Main () [0x00000] in <092e5ea1577d4af6be34040e9472afab>:0 

Article Tags :
C#