3

I'm trying to figure out of there is a somewhat straight forward way to get the reflected properties of the currently executing async method. I'm looking for the same properties that wuld be available if I were to just do something like this in a regular non-async method:

var method = (MethodInfo)MethodBase.GetCurrentMethod();

But if I'm in an async method, I am getting a MoveNext() method back from reflection, when using the above reflective code.

Would I just have to step up the call stack using StackTrace or StackFrame?

The only problem that I have with that is that it's somewhat manual. This code needs to by dynamic, as my implementation is going to be used in logging, and needs to be reliable and accurate across many applications. It needs to work with both async and synchronous methods / functions.

All that I can find out there currently is people using the baked in .net CallermemberName attribute, which gets the correct name of the currently executing method, but obviously I need more than that.

Any help would be greatly appreciated. Thanks!

ganjeii
  • 1,168
  • 1
  • 15
  • 26
  • Which logger are you going to use? Nlog has the current method built in https://stackoverflow.com/questions/21949078/adding-method-name-in-nlog – Neil Sep 25 '19 at 15:06
  • I'm using Serilog, Unfortunately NLog is not an option. – ganjeii Sep 25 '19 at 18:49

1 Answers1

2

It depends on which logger you are going to use, for example NLog already has this functionality, but as mentioned on their web site, using it for async functions infers heavy performance hit, probably due to walking the stack.

Neil
  • 11,059
  • 3
  • 31
  • 56