Intercept the call to an async method using DynamicProxy

Presumably the “problem” is that it’s just logging that it’s returning a task – and you want the value within that task?

Assuming that’s the case, you still have to return the task to the caller, immediately – without waiting for it to complete. If you break that, you’re fundamentally messing things up.

However, before you return the task to the caller, you should add a continuation (via Task.ContinueWith) which will log the result (or failure) when the task completes. That will still give the result information, but of course you’ll be logging it potentially after some other logging. You may also want to log immediately before returning, leading to a log something like this:

Called FooAsync
Returned from FooAsync with a task
Task from FooAsync completed, with return value 5

The business of getting the result out of the task (if it completed successfully) would have to be done with reflection, which is a bit of a pain – or you could use dynamic typing. (Either way it will be a bit of a performance hit.)

Leave a Comment