There's a significant performance hit when using this approach to get the name of the current method, so I wouldn't recommend it.
People often mention the CallerMemberNameAttribute
class, but this often doesn't play well with now you'd want to use a logging interface. The reason for this is it's typlically applied as a default parameter, which means it has to go to the right of the non default parameters. A lot of logging API have the format:
Info(string format, params object[] values)
Now, adding a parameter that has the CallerMemberNameAttribute
is going to be a problem. You can't put it on the right as the params
parameters prohibit this, and you can't put it anywhere else as it won't be eligable to be a default parameter. One option would be to change your logging API to something like this:
Info(string format, object[] values, [CallerMemberName] string memberName = "")
But then you have to explicitly create an array for the values, which tends not to flow as well.