使用环境类获取当前堆栈跟踪信息的 C# 程序
在 C# 中,环境类提供有关当前平台的信息并操作当前平台。它对于获取和设置各种与操作系统相关的信息很有用。我们可以使用它来检索命令行参数信息、退出代码信息、环境变量设置信息、调用堆栈信息的内容以及自上次系统启动以来的时间(以毫秒为单位)信息。通过使用一些预定义的方法,我们可以使用 Environment 类获取操作系统的信息。在本文中,我们使用StackTrace属性来获取当前堆栈跟踪信息。该属性用于获取当前堆栈跟踪信息
语法:
string Environment.StackTrace
返回:字符串并给出类名和 id
示例 1:
C#
// 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);
}
}
C#
// 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);
}
}
输出:
Current Stack Trace Details:
at System.Environment.get_StackTrace () [0x00000] in :0
at GFG.Main () [0x00000] in :0
示例 2:
C#
// 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);
}
}
输出:
Current Stack Trace Details:
at System.Environment.get_StackTrace () [0x00000] in :0
at GFG.Main () [0x00000] in <092e5ea1577d4af6be34040e9472afab>:0