0

I would like to list down the method call sequence from top to bottom in my .net project.

    using System;
                        
    public class Program
    {
    public static void Main()
    {
        One();
    }
    
    public static void One(){
        Two();
    }
    
    public static void Two(){
        Three();
    }
    
    public static void Three(){
        Four();
    }
    
    public static void Four(){
        Console.WriteLine("Hello World!");
    }
}

In the above sample class I required to get the log like "Main()->One()->Two()->Three()->Four()"

Guna
  • 113
  • 3
  • 9

2 Answers2

0

you can use System.Diagnostics.StackTrace to get the current stackTrace and use it to log the required:

Demo as per your code:

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello World");
        One();
    }
    
    public static void One(){
        Two();
    }
    
    public static void Two(){
        Three();
    }
    
    public static void Three(){
        Four();
    }
    
    public static void Four(){
        Console.WriteLine("Hello World!");
        var stackTrace = new System.Diagnostics.StackTrace();
        List<string> methods = new List<string>();
        for (int i = stackTrace.FrameCount - 1; i >= 0; i--)
        {
            methods.Add($"{stackTrace.GetFrame(i).GetMethod().Name}()");
        }
        
        Console.WriteLine(string.Join("->", methods));
    }
}

The above code prints the output as

Main()->One()->Two()->Three()->Four()

Check the fiddle - https://dotnetfiddle.net/Ee8ni8

user1672994
  • 10,509
  • 1
  • 19
  • 32
  • Thanks for your quick response, in this approach we may have to log the stack trace at end of last method. In my scenario I'm having over 250 wcf methods. – Guna Jan 18 '21 at 06:34
  • You should not be doing this in every method of yours. This should be a clubbed with your logging functionality as cross cutting concern. So any Log statement from anywhere in your code will output the log with stacktrace along with message. If you want to enable it only for specific classes/namespace/assemblies then that can be controlled by some configuration (which you would need to write that). – user1672994 Jan 18 '21 at 06:38
0

There is one non-ideal solution:

  • Prepare a set of test cases that will run your program methods in as many combinations as possible, and so provide good code coverage.
  • Manually add a stack trace logging functionality at the beginning of each method's body and adjust stack trace logging format to your needs (Main()->One()...). This could be done with System.Diagnostics.StackTrace, as user1672994 pointed out.
  • Filter your stack trace log so that it contains only stack traces which are not prefix of any other stack traces, thus leaving off incomplete paths.

You could also discover the call flow from Call Hiearchy window in Visual Studio, but it doesn't give you the data in requested format. It will however find call flows independently of your possibly imperfect test cases.

See also: Visual Studio Call Hierarchy View: call it programmatically

user14967413
  • 1,264
  • 1
  • 6
  • 12