Castle Windsor Interceptor w/o Interface?

929 views Asked by At

Using Castle Windsor for DI, I've two classes that implement the same interface; and besides that have some methods and properties of their own. I'm using Castle DynamicProxy and created an Interceptor that'll do some logging w/Log4Net via the Castle.Facilities.LoggingLoggingFacility.

The log file neatly logs for each method that's implemented via the interface, when that method gets called. In the sample code below, method Foo() gets logged, when called, but method LogMeToo() doesn't since it isn't part of the implementation of IFoo.

I want that also the other methods that don't implement the interface get logged when called. Is this possible, and if so: how?

public interface IFoo
{
  void Bar();
}
[Interceptor(typeof(LoggingInterceptor))] public class Foo : IFoo { public void Bar() { // Do Something }
public void LogMeToo() { // Do Something } }
public static class Program { [STAThread] public static void Start() { var container = new WindsorContainer();
container.Register(Component.For<LoggingInterceptor>().LifeStyle.Transient); container.Register(Component.For<IFoo>().ImplementedBy<Foo>());
container.AddFacility<LoggingFacility>(f => f.LogUsing(LoggerImplementation.Log4net).WithConfig("Log4net.config")); } }
WPF C# 4.0

1

There are 1 answers

1
Krzysztof Kozmic On

No, it's not possible unless you make the methods virtual and expose the class as a (first) service so that class proxy gets generated. However this smells as broken abstraction. If you're exposing Foo as IFoo then LogMeToo will never get called anyway - no need to log anything.