开发者

Castle Windsor Interceptor w/o Interface?

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 whe开发者_JAVA技巧n 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜