I am looking to create a filter implementing IAsyncActionFilter
that will retrieve data from the current request's context's ActionParameters
and its Result
. I'm using a custom attribute MyLogAttribute
to direct the logging beheavior, e.g. to opt-in for logging and indicating fields with critical information.
public class AsyncMyLogFilter : IAsyncActionFilter
{
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
var actionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;
if (actionDescriptor != null)
{
var attribute = actionDescriptor.MethodInfo.GetCustomAttribute<MyLogAttribute>();
if (attribute != null)
{
await next();
// This is where the magic is supposed to happen:
LoggerHelper.Log(context.ActionArguments, context.Result);
}
return;
}
await next();
}
}
The way the filter provides a next()
delegate led me to believe that past that point the action would be completed and the result object available for inspection as an ObjectResult
. However, while the filter is able to grab the ActionArguments
without problem, the Result
property is unfortunately just null, which is not helpful at all.
The obvious alternative, the synchronous IActionFilter
, lets me examine the Result
object on the OnActionExecuted
stage, but at this point the ActionArguments
dictionary is not available.
So, is there any way to access both ActionArguments
and Result
within the same method scope?
-S
While the problem in its original form has not been resolved, I was able to create a workaround with an
IActionFilter
:Not exactly rocket science; it looks kinda flimsy ("What if my HttpContext items go missing!?"), but it seems to do the job.