[英]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:有一个非理想的解决方案:
Main()->One()...
).Main()->One()...
)。 This could be done with System.Diagnostics.StackTrace
, as user1672994 pointed out .System.Diagnostics.StackTrace
来完成。 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.