So for everyone who responded to my original thread, you may be interested in what I came up with so far. Maybe you guys, or somebody else, can add to this discussion and we can find a more permanent solution.
But, here's the closest I got to automating this:
1) Create this function/method and add it to every class:
public void LogInfo(string className, string methodName)
{
string info = ("In class: " + className + " In method: " + methodName);
Console.WriteLine(info);
}
2) Paste this line into the relevant areas of the code base:
StackTrace stackTrace = new StackTrace();
LogInfo(MethodBase.GetCurrentMethod().DeclaringType.Name, stackTrace.GetFrame(0).GetMethod().Name);
3) Also please take a look at my modified code, and note that we need to add these two using cases:
using System.Diagnostics;
using 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) The output now becomes:
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) A few things I'd like to point out to you guys: I thought the commented out for loop would be the magic bullet that would solve everything in one line, but it gives the output:
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
Also note the commented out line, in class C:
If you uncomment it, it gives you the entire execution flow properly (won't post results because it includes personal info). However, this means I need to dig deep within the code, find the method that is called last, and add this line of code to it.
6) Sources:
How performant is StackFrame?
How can I find the method that called the current method?
C# getting its own class name
Please let me know what you guys think. Thanks.