所以,对于每个回复我最初帖子的人来说,你可能会对我到目前为止的想法感兴趣。也许你们,或者其他人,可以加入这个讨论,我们可以找到一个更持久的解决方案。
但是,这里是我最接近自动化的地方:
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) 另外,请看一下我修改的代码,注意我们需要添加以下两个用例:
使用系统。诊断学;
使用系统。反射
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类中注释掉的行:
post执行(如果你没有给出完整的个人信息,那么它就没有给出完整的结果)。然而,这意味着我需要深入挖掘代码,找到最后调用的方法,并将这行代码添加到其中。
6) 资料来源:
How performant is StackFrame?
How can I find the method that called the current method?
C# getting its own class name
请告诉我你们的想法。谢谢