繁体   English   中英

如何在C#中打印程序的执行流程

[英]How to print the flow of execution of a program in c#

我试图了解如何打印程序的执行流程。 我写了一些简单的代码来证明我的困境。

我想要打印以下内容:在A类中,Main。 在类B中,方法B。 在类C中,方法C。

StackTrace似乎给了我很多难以阅读的信息。 有没有更简单的方法,或者有一种更容易阅读StackTrace的方法? 实际的代码要大得多,并且我不想在每个方法中都使用print语句,因此我认为这样可以使调试更加容易。 另外,我无法在实际代码中使用Visual Studio的调试器。 因此,如果你们可以建议我使用哪个功能(如果不是StackTrace的话),它将为我打印必要的信息,我将不胜感激。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StackTrace
{
        public class A
        {
            public static void Main(string[] args)
            {
                B newB = new B();
                newB.methodB();
                Console.WriteLine("StackTrace: {0}", Environment.StackTrace); //I'd like to get the entire execution flow printed here
                Console.ReadKey(); //just for testing, don't worry about this.
            }
        }

        public class B
        {

            public void methodB()
            {
                C newC = new C();
                //Console.WriteLine("In class B, method B");
                newC.methodC();
            }
        }

        public class C
        {
            public void methodC()
            {
                //Console.WriteLine("In class C, method C");
            }
        }
}

因此,对于对我的原始主题做出回应的每个人,您可能会对到目前为止我的想法感兴趣。 也许你们或其他人可以加入讨论,我们可以找到一个更永久的解决方案。 但是,这是我最接近的自动化方法:

1)创建此函数/方法并将其添加到每个类中:

public void LogInfo(string className, string methodName)
{
   string info = ("In class: " + className + " In method: " + methodName);
   Console.WriteLine(info);
}

2)将此行粘贴到代码库的相关区域中:

 StackTrace stackTrace = new StackTrace();
 LogInfo(MethodBase.GetCurrentMethod().DeclaringType.Name, stackTrace.GetFrame(0).GetMethod().Name);

3)另外,请查看我的修改后的代码,并注意我们需要添加这两个用例:
使用System.Diagnostics; 使用System.Reflection;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


using System.Diagnostics;
using System.Reflection;

namespace StackTraceTest
{
        public class A
        {
            public static void Main(string[] args)
            {
                StackTrace stackTrace = new StackTrace();
                LogInfo(MethodBase.GetCurrentMethod().DeclaringType.Name, stackTrace.GetFrame(0).GetMethod().Name);

                B newB = new B();
                newB.methodB();

                LogInfo(MethodBase.GetCurrentMethod().DeclaringType.Name, stackTrace.GetFrame(0).GetMethod().Name);

                /*for (int i = 0; i < stackTrace.FrameCount;i++)
                {
                    LogInfo(stackTrace.GetFrame(i).GetType().Name, stackTrace.GetFrame(i).GetMethod().Name);
                }*/

                Console.ReadLine();
            }

            public static void LogInfo(string className, string methodName)
            {
                string info = ("In class: " +className +" In method: " +methodName);
                Console.WriteLine(info);
            }
        }

        public class B
        {

            public void methodB()
            {
                StackTrace stackTrace = new StackTrace();
                LogInfo(MethodBase.GetCurrentMethod().DeclaringType.Name, stackTrace.GetFrame(0).GetMethod().Name);

                C newC = new C();
                newC.methodC();
            }

            public void LogInfo(string className, string methodName)
            {
                string info = ("In class: " + className + " In method: " + methodName);
                Console.WriteLine(info);
            }
        }

        public class C
        {
            public void methodC()
            {
                StackTrace stackTrace = new StackTrace();
                LogInfo(MethodBase.GetCurrentMethod().DeclaringType.Name, stackTrace.GetFrame(0).GetMethod().Name);
                //Console.WriteLine("StackTrace: {0}", Environment.StackTrace);
            }

            public void LogInfo(string className, string methodName)
            {
                string info = ("In class: " + className + " In method: " + methodName);
                Console.WriteLine(info);
            }
        }
}

4)现在的输出变为:

In class: A In method: Main
In class: B In method: methodB
In class: C In method: methodC
In class: A In method: Main

5)我想向大家指出一些事情:我认为注释掉的for loop将是可以解决所有问题的魔术子弹,但是它提供了输出:

In class: StackFrame In method: Main
In class: StackFrame In method: _nExecuteAssembly
In class: StackFrame In method: ExecuteAssembly
In class: StackFrame In method: RunUsersAssembly
In class: StackFrame In method: ThreadStart_Context
In class: StackFrame In method: RunInternal
In class: StackFrame In method: Run
In class: StackFrame In method: Run
In class: StackFrame In method: ThreadStart

还要注意在C类中的注释行:如果取消注释,它将为您提供正确的整个执行流程(不会发布结果,因为它包含个人信息)。 但是,这意味着我需要深入研究代码,找到最后调用的方法,并将这行代码添加到其中。

6)资料来源: StackFrame的性能如何?

如何找到调用当前方法的方法?

C#获得自己的类名

请让我知道你们的想法。 谢谢。

使用免费版本的postharp,您可以很容易地做到这一点。 首先,创建一个可以标记到方法上的属性:

[PSerializable]
public class LogInvocationAttribute : MethodInterceptionAspect
{
    public override void OnInvoke(MethodInterceptionArgs args)
    {
        // TODO: use your existing logging method here. simple example here for now.
        Console.WriteLine($"{args.Instance.GetType()}.{args.Method.Name}");             

        // pass the call through to the original receiver
        args.Proceed();
    }
}

之后,您要记录的任何方法都可以仅应用此属性,如下所示:

[LogInvocation]
public void SomeMethod(int someArg)
{
    // do stuff
}

使用付费版本的postharp,您实际上可以在基类级别上应用类似的东西,并将其也自动应用到子类(SO参考此处 ),但是对于您想要执行的操作,将属性应用于方法是一种简单的方法你想要什么。

暂无
暂无

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

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