Search code examples
aopautofaccastle-dynamicproxy

AOP with Autofac and DynamicProxy2


I'm using Autofac with DynamicProxy2 to intercept my classes like this

builder.RegisterType<Calculator>().As<ICalculator>().EnableInterfaceInterceptors()
                .InterceptedBy(typeof (CallLogger));

This will intercept all methods on given interface.

Is there any way to intercept only particular methods of the interface ?


Solution

  • There are a couple of ways I can think of, and there are probably more out there.

    You could create a custom attribute like [LogCall] or [DoNotLogCall] (opt in or opt out) and apply that to the methods to intercept, then check for the presence of the attribute inside CallLogger.

    Or, you could configure the CallLogger with the names of (or rules to find) the methods that should be logged.