rodchar said:
is there a way to retrieve the name of the method that the current thread is
executing in?
I know of 3 options:
1)
MethodBase method = (new StackTrace(true)).GetFrame(0).GetMethod();
string methodname = method.DeclaringType.FullName + "." + method.Name;
2)
MethodBase method = MethodInfo.GetCurrentMethod();
string methodname = method.DeclaringType.FullName + "." + method.Name;
3)
MethodTracker.GetCurrentMethod()
where MethodTracker is the code below and weaved in via
AspectDNG.
Arne
==========================================
using System;
using System.Collections.Generic;
using System.Threading;
using DotNetGuru.AspectDNG.Joinpoints;
public class MethodTracker
{
private static Dictionary<int, Stack<string>> methods = new
Dictionary<int, Stack<string>>();
public static string GetCurrentMethod()
{
return methods[Thread.CurrentThread.ManagedThreadId].Peek();
}
[AroundCall("* YourNameSpace.YourClass::*(*)")]
public static object SaveMethod(JoinPoint jp)
{
if(jp is MethodJoinPoint)
{
if(!methods.ContainsKey(Thread.CurrentThread.ManagedThreadId))
{
methods.Add(Thread.CurrentThread.ManagedThreadId, new
Stack<string>());
}
MethodJoinPoint mjp = (MethodJoinPoint)jp;
methods[Thread.CurrentThread.ManagedThreadId].Push(mjp.TargetOperation.DeclaringType.FullName
+ "." + mjp.TargetOperation.Name);
}
object res = jp.Proceed();
if(jp is MethodJoinPoint)
{
methods[Thread.CurrentThread.ManagedThreadId].Pop();
}
return res;
}
}