温莎城堡不会在物业注入记录仪! [英] Castle Windsor won't inject Logger in a property!

查看:183
本文介绍了温莎城堡不会在物业注入记录仪!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着在我的服务类的ILogger属性注入log4net的,但物业始终是NULL!

I try to inject log4net in a ILogger property of my service class but the property is always NULL!

我已经看到了这个话题,但它并不能帮助我!

I've seen this topic but it doesn't help me!

我怎样才能得到温莎城堡到自动注入的属性?

这是Program.cs的

this is Program.cs

 CastleContainer.Instance
        .Install(
          new RepositoriesInstaller(),
          new PersistenceInstaller(),
          new LoggerInstaller(),
          new FormInstaller(),
          new ServiceInstaller()

          );

        FrmStart form1 = CastleContainer.Resolve<FrmStart>(new {Id="666" });



我用log4net.config外部文件,这是我的安装​​程序:

I use log4net.config external file and this is my installer:

public class LoggerInstaller : IWindsorInstaller
{
    #region IWindsorInstaller Members

    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
         container.AddFacility("logging", new LoggingFacility(LoggerImplementation.Log4net, "log4net.config"));
    }

    #endregion
}

这是类包含我想温莎注入属性:

This is the class contains the property I want Windsor to inject:

public partial class FrmStart : Form
{
    private EventService EventService;

    private ILogger logger = NullLogger.Instance;
    public ILogger Logger
    {
        get { return logger; }
        set { logger = value; }
    }

    public FrmStart(EventService eventService, string Id)
        : this()
    {

        Logger.Debug("xxx");

        this.EventService = eventService;
        this.id = Id;
    }

请注意,在构造函数eventService和ID被正确注射!
。如果我尝试注入在它的工作原理构造的记录器,我已经在Logger对象:
{log4net.Repository.Hierarchy.DefaultLoggerFactory + LoggerImpl}! : - !。(

Note that "eventService" and "Id" in the constructor are correctly injected! If I try to inject the Logger in the constructor it works and I've the Logger object: {log4net.Repository.Hierarchy.DefaultLoggerFactory+LoggerImpl}! :-(

我试图创造EventService和温莎公共财产可以适当注入,所以我认为这个问题是只涉及到ILogger接口

I've tried to create a public property for EventService and Windsor can inject it properly! So I think the problem is related only to the ILogger interface.

我准备在这里一个简单的全代码示例:

I prepared a simple full-code example here:

using Castle.Core.Logging;
using Castle.Facilities.Logging;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor;

namespace IocTest
{


public class LoggerInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
         container.AddFacility("logger", new LoggingFacility(LoggerImplementation.Log4net, "log4net.config"));
    }
}
public class LogicInstaller : IWindsorInstaller
{
     public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(AllTypes.FromThisAssembly()
                            .Pick()
                            .If(t => t.Name.StartsWith("Logic"))
                            .Configure((c => c.LifeStyle.Transient)));
    }
}

class Program
{
    static void Main(string[] args)
    {
        IWindsorContainer container = new WindsorContainer();

        container.Install(
        new LoggerInstaller(),
          new LogicInstaller()
          );


        LogicClass1 logic1 = container.Resolve<LogicClass1>();
        LogicClass2 logic2 = container.Resolve<LogicClass2>();
    }
}

public class LogicClass1
{
    private ILogger logger = NullLogger.Instance;
    public ILogger Logger
    {
        get { return logger; }
        set { logger = value; }
    }

    public LogicClass1()
    {
        logger.Debug("Here logger is NullLogger!");
    }
}

public class LogicClass2
{
    public LogicClass2(ILogger logger)
    {
        logger.Debug("Here logger is properly injected!");
    }
}
}



怎么了?

What's wrong?

推荐答案

一个问题是,你正在检查它:

A problem is where you are checking it:

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

    public LogicClass1()
    {
        logger.Debug("Here logger is NullLogger!");
    }



属性注入将不会发生,直到的之后的了构造函数运行,所以在检查构造属性值永远不会告诉你期待

The property injection will not happen until after the constructor is run, so checking the property value in the constructor will never show the value you are expecting

这篇关于温莎城堡不会在物业注入记录仪!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆