I'm using webapi2 and Castle Windsor. I'm trying to intercept calls to the ApiController to make some logging, but I can't find in the parameter the method called, url, parameters, etc. The method name returned is ExecuteAsync.
This is my interceptor call (which is hit)
public class LoggingInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
var methodName = invocation.Method.Name;
invocation.Proceed();
}
}
if (invocation.Method.Name == "ExecuteAsync")
{
var ctx = (HttpControllerContext) invocation.Arguments[0];
Console.WriteLine("Controller name: {0}", ctx.ControllerDescriptor.ControllerName);
Console.WriteLine("Request Uri: {0}", ctx.Request.RequestUri);
}
invocation.Proceed();
Intercepting the ExecuteAsync call, you can get access to the HttpControllerContext but not the HttpActionContext, which contains details about the actual controller action method being invoked. (As far as I can tell this is only available to an ActionFilter).
Therefore the detail about the request is limited: for instance, deserialized parameters are not available using this technique.
If you wish, you can make your controller action methods virtual
and these will then also be intercepted, and you can then access the actual action parameters via invocation.arguments
. However, in your interceptor you will need to somehow differentiate the action methods that you wish to log, from the other methods that get called on the controller (i.e. Intialize()
, Dispose()
, ExecuteAsync()
, Ok()
, etc.)