EDIT: Okay, I think I misunderstood you.
Basically, you need to remember the Task
that you've started, and then you can just call Wait()
to wait until it's finished:
class LogMethodCallAttribute : OnMethodBoundaryAspect
{
Task task;
... as before ...
public override void OnEntry(MethodExecutionArgs args)
{
...
task = Task.Factory.StartNew(...);
}
public override void OnExit(MethodExecutionArgs args)
{
exitTime = DateTime.Now;
// Wait for the task to have completed...
task.Wait();
// Now you can use the fields
}
}
Personally I would actually not just set the fields in the task though - I'd make the task a Task<T>
for some T
which contains the logging parameters. Then you can make the only field in your attribute the task, and have:
var loggingParameters = task.Result;
... use the logging parameters