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

时间:2018-05-18 17:08:03

标签: c# embedded execution

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

我希望打印这样的内容: 在A级,主要。 在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");
            }
        }
}

2 个答案:

答案 0 :(得分:1)

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

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循环将是解决一行中所有问题的神奇子弹,但它会给出输出:

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)来源: How performant is StackFrame?

How can I find the method that called the current method?

C# getting its own class name

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

答案 1 :(得分:1)

使用免费版本的postsharp,您可以轻松完成此操作。首先,创建一个可以标记到方法的属性:

[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
}

对于postsharp的付费版本,您实际上可以在基类级别应用此类内容,并将其自动应用于子级(SO参考here),但是您想要将属性应用于您的方法是获得你想要的东西的简单方法。

相关问题