Reputation: 117
I've been focusing on logging more lately. I really do appreciate getting the method name in the logger. My only issue is that the reflective method is so long to write.
I tested with static classes and a method for it. But to my not so big surprise, it gave me the name of the static class method.
So far the only way I found to shorten it a bit is
using Info = System.Reflection.MethodBase;
string name = Info.GetCurrentMethod().Name;
Is there a way to shorten this to something like Info.MethodName()
?
Upvotes: 0
Views: 189
Reputation: 117
After following tips from Leisen Chang tips about using CallerMemberName solved it with a static class.
public static class Info
{
public static string MethodName([System.Runtime.CompilerServices.CallerMemberName] string name = "")
{
return name;
}
}
Upvotes: 1