Is there anyway to get the current method name from inside an async function?
I've tried:
System.Reflection.MethodInfo.GetCurrentMethod();
And I've tried using StackTrace and StrackFrame as follows:
StackTrace strackTrace = new StackTrace();
for (int i = 0; i < strackTrace.GetFrames().Length; i++)
{
SafeNativeMethods.EtwTraceInfo("Function" + i + ":" + function);
SafeNativeMethods.EtwTraceInfo("Type" + i + ":" + strackTrace.GetFrame(i).GetType().Name);
SafeNativeMethods.EtwTraceInfo("Method" + i + ":" + strackTrace.GetFrame(i).GetMethod().Name);
SafeNativeMethods.EtwTraceInfo("ToString Call" + i + ":" + strackTrace.GetFrame(i).ToString());
}
But neither of them seem to work, I'd get ".ctor", "InvokeMethod", "Invoke", "CreateInstance", "CreateKnownObject" or "CreateUnknownObject" or "MoveNext"
Any ideas on how I can do this? I want to create a generic logger function and I don't want to pass in the name of the function that called the logger function, so I tried the stacktrace method, didn't work.
I gave up on that and said, ok, I'll pass in the function name as the first parameter, but when I called the reflection method from the calling function that calls the generic logger function, I always get ".ctor"
Any ideas? Note the generic logger function I'm calling is a static method in the same class (it has to be this way for now...).
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…