C# Program to Get All Stack Frames using StackTrace Class

This is a C# Program to create an instance of stacktrace and to get all frames.

Problem Description

This C# Program Creates an Instance of StackTrace and to Get all Frames.

Problem Solution

Here It creates an instance of StackTrace (call stack), gets all frames (method calls) and writes the method names.

Program/Source Code

Here is source code of the C# Program to Create an Instance of StackTrace and to Get all Frames. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Create an Instance of StackTrace and to Get all Frames
 */
using System.Diagnostics;
using System;
class program
{
    public static void Main()
    {
        StackTrace stackTrace = new StackTrace();           
        StackFrame[] stackFrames = stackTrace.GetFrames();  
        // write call stack method names
        Console.WriteLine("Method Names : ");
        foreach (StackFrame stackFrame in stackFrames)
        {
            Console.WriteLine(stackFrame.GetMethod().Name);   
        }
        Console.Read();
    }
}
Program Explanation

This C# Program is used to create an instance of StackTrace and to get all frames. Here it creates an instance of StackTrace (call stack), gets all frames (method calls) and writes the method names. Using foreach statement, print the frames.

advertisement
advertisement
Runtime Test Cases
 
Method Names : 
Main
nExecuteAssembly
ExecuteAssembly
RunUsersAssembly
ThreadStart_Context
Run
ThreadStart

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.