views:

35

answers:

0

I'm trying to figure out how to setup logging in Castle Windsor. I cannot find anywhere how to do this. I have the following code for setting up logging:

public class Installer : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        InstallLog(container);
    }

    private void InstallLog(IWindsorContainer container)
    {
        container.Register(
            Component
                .For<ILogger>()
                .Named("Logger")
                .Instance(new Log4netFactory("Log4Net.xml")
                .Create("root")));
    }
}

In the code above, I'm not sure what parameters in Named() and Create() are used for. And another class for creating an instance of a WCF service.

public class FormServiceHostFactory : DefaultServiceHostFactory
{
    public FormServiceHostFactory() : base(CreateKernel())
    {
    }

    private static IKernel CreateKernel()
    {
        var container = new WindsorContainer();
        container
            .AddFacility<WcfFacility>()
            .Register(
                Component
                    .For<FormService>()
                    .Named("FormService")
                    .LifeStyle.Transient);

        return container.Kernel;
    }
}

public class FormService : IFormService
{
    private ILogger logger = NullLogger.Instance;

    public ILogger Logger
    {
        get { return logger; }
        set { logger = value; }
    }

    public void TestOp()
    {
        logger.Info("Hi there!");
    }
}

Now from my understanding, Windsor is supposed to automatically inject Logger property in class FormService. But when I trace the code, I see that FormService is using NullLogger. What am I missing here?

Update: After further Investigation, I found something interesting. When I register both WCF service at the same place where I register ILogger, Windsor injects Logger property of FormService just fine. But when I inherit from DefaultServiceHostFactory to register FormService, it doesn't inject Logger property with Log4netLogger. So it works when I removed FormServiceHostFactory and change the Installer code like below:

private void InstallLog(IWindsorContainer container)
    {
        container.Register(
            Component
                .For<ILogger>().Named("Logger")
                .Instance(new Log4netFactory("Log4Net.xml").Create("root"))
            );

        container.AddFacility<WcfFacility>()
            .Register(
                Component
                    .For<FormService>()
                    .Named("FormService")
                    .LifeStyle.Transient);

    }