ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

C# program to get all stack frames using StackTrace class

Here, we are going to learn how to get all stack frames using StackTrace class in C#?
Submitted by Nidhi, on November 05, 2020

Here, we will get stack frames(method name, module name) using StackTrace and StackFrame class.

Program:

The source code to get all stack frames using StackTrace class is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to get all stack frames using StackTrace class

using System;
using System.Diagnostics;

class Demo
{
    public static void Main()
    {
        StackTrace trace = new StackTrace();
        StackFrame[] frames;
        
        frames= trace.GetFrames();
        
      
        Console.WriteLine("Frames: ");
        foreach (StackFrame frame in frames)
        {
            Console.WriteLine("\tMethod Name: "+frame.GetMethod().Name);
            Console.WriteLine("\tModule Name: "+frame.GetMethod().Module+"\n");
        }
    }
}

Output:

Frames:
        Method Name: Main
        Module Name: Test.exe

Press any key to continue . . .

Explanation:

In the above program, we created a Demo class that contains the Main() method, The Main() method is the entry point for the program, here, we created the object of StackTrace class and the get the stack frames using GetFrames() method. The GetFrames() returns the array of StackFrame. Then we access the frame one by one using the "foreach" loop. Here we printed the method names and module names on the console screen.

ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.