如何通过NServiceBus中的Castle.Windsor注入Log4Net日志记录

时间:2013-04-10 12:59:18

标签: c# castle-windsor log4net nservicebus

我希望NServiceBus使用Castle.Windsor容器提供的记录器,而不是直接提供它。有没有办法实现这个目标?以下不作为开始时提出的例外。

public class EndPointConfig : IConfigureThisEndpoint, IWantCustomInitialization, IWantCustomLogging
{
    private ILogger _logger = NullLogger.Instance;
    public ILogger Logger
    {
        get { return _logger; }
        set { _logger = value; }
    }


    public void Init()
    {
        SetLoggingLibrary.Log4Net(null, Logger);
        Configure.With()
            .CastleWindsorBuilder(Host.Init.CreateSubsystems())
            .RunTimeoutManager()
            .UseNHibernateTimeoutPersister();
    }
}

Host.Init.CreateSubsystems()返回Castle.Windsor容器,并带有注册记录器。

1 个答案:

答案 0 :(得分:1)

如果您提供带有注册到CastleWindsorBuilder的ILogger的容器alreday bootstrap,则无需执行任何其他工作:NServiceBus将根据log4net配置使用您的记录器设置(单独的文件或根据记录器注册在app.config中)在容器中)

SetLoggingLibrary.Log4Net(null,Logger);不需要。

public class MessageEndpoint : IConfigureThisEndpoint, AsA_Server, IWantCustomInitialization
{
    public void Init()
    {
        var container = new WindsorContainer();
        var installerFactory = new MyInstallerFactory();
        container.Install(FromAssembly.This(installerFactory));

        var logger = container.Resolve<ILogger>();
        logger.Debug("Container bootstrapped");

        Configure.With()
                 .DisableTimeoutManager()
                 .CastleWindsorBuilder(container)
                 .JsonSerializer();

        logger.Debug("Bus configured");
    }
}