简体   繁体   English

Visual Studio 方法调用流转储

[英]Visual Studio Method Call Flow Dump

I would like to list down the method call sequence from top to bottom in my .net project.我想在我的 .net 项目中从上到下列出方法调用顺序。

    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()"在上面的示例 class 中,我需要获取类似“Main()->One()->Two()->Three()->Four()”的日志

you can use System.Diagnostics.StackTrace to get the current stackTrace and use it to log the required:您可以使用System.Diagnostics.StackTrace获取当前 stackTrace 并使用它来记录所需的:

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上面的代码将 output 打印为

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

Check the fiddle - https://dotnetfiddle.net/Ee8ni8检查小提琴 - https://dotnetfiddle.net/Ee8ni8

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()... ).在每个方法主体的开头手动添加堆栈跟踪日志记录功能,并根据您的需要调整堆栈跟踪日志记录格式( Main()->One()... )。 This could be done with System.Diagnostics.StackTrace , as user1672994 pointed out .正如 user1672994 所指出的,这可以通过System.Diagnostics.StackTrace来完成。
  • 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.您还可以从 Visual Studio 中的Call Hiarchy window中发现调用流程,但它不会以请求的格式为您提供数据。 It will however find call flows independently of your possibly imperfect test cases.但是,它会独立于您可能不完美的测试用例找到呼叫流程。

See also: Visual Studio Call Hierarchy View: call it programmatically另请参阅: Visual Studio 调用层次结构视图:以编程方式调用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM