[英]Castle Windsor won't inject Logger in a property!
I try to inject log4net in a ILogger property of my service class but the property is always NULL!我尝试在我的服务 class 的 ILogger 属性中注入 log4net,但该属性始终为 NULL!
I've seen this topic but it doesn't help me!我看过这个话题,但它对我没有帮助!
How can I get Castle Windsor to automatically inject a property? 如何让 Castle Windsor 自动注入属性?
this is Program.cs这是 Program.cs
CastleContainer.Instance
.Install(
new RepositoriesInstaller(),
new PersistenceInstaller(),
new LoggerInstaller(),
new FormInstaller(),
new ServiceInstaller()
);
FrmStart form1 = CastleContainer.Resolve<FrmStart>(new {Id="666" });
I use log4net.config external file and this is my installer:我使用 log4net.config 外部文件,这是我的安装程序:
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:这是 class 包含我希望温莎注入的属性:
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;
}
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.请注意,构造函数中的“eventService”和“Id”已正确注入:如果我尝试在构造函数中注入 Logger,它可以工作,并且我有 Logger object。 {log4net.Repository.Hierarchy:DefaultLoggerFactory+LoggerImpl}!
{log4net.Repository.Hierarchy:DefaultLoggerFactory+LoggerImpl}! :-(
:-(
I've tried to create a public property for EventService and Windsor can inject it properly.我尝试为 EventService 创建一个公共属性,Windsor 可以正确注入它。 So I think the problem is related only to the ILogger interface.
所以我认为问题只与 ILogger 接口有关。
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直到构造函数运行后才会发生属性注入,因此检查构造函数中的属性值将永远不会显示您期望的值
I was having the same problem.我遇到了同样的问题。 It was always
null
.它总是
null
。
I managed to solve the problem by injecting the logger in the constructor this way:我设法通过以这种方式在构造函数中注入记录器来解决问题:
public ILogger logger;
public MyController(ILogger logger)
{
this.logger = logger;
logger.Info("Something");
}
You could also initialize your Logger by using:您还可以使用以下方法初始化 Logger:
public ILogger Logger { get; set; }
public MyController()
{
Logger = NullLogger.Instance;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.