I'm loading a ninject module in my (WCF) project and want to hook up log4net in my module using a custom provider. The reason I'm using a provider is that the GetLogger method needs to get passed the class in which the Injection is taking place. If this is not the way to do this, please let me know, I'm new to Ninject. (Context.getType() is not correct btw, I'm having some trouble doing this.)
The main problem is that my ILog variables in my classes get Initialized after the constructor has executed, but I want to be able to use the logging in my constructor. I am also using Ninject to call my constructor and bind it's parameters to concrete classes.
Bind<IBroker>().To<Broker>(); // constructor
Bind<ILog>().ToProvider(new log4netILogProvider()) // property
private class log4netILogProvider : Ninject.Activation.Provider<ILog>
{
protected override ILog CreateInstance(IContext context)
{
return LogManager.GetLogger(context.getType());
}
}
All help is appreciated.
No IoC framework will ever be able to do property injection before constructor injection. An IoC framework can only do the same things you can do in a manually written factory. You can't access a property of an object in a factory before it has been created right? So can't the IoC framework.
Basically you have two options.
Do method injection and execute your constructor code in the method.
[Inject]
public void Initialize(ILog log) { ... }