I have gone through some other posts in this forum that are related to Ninject and Log4net, but none seemed to address the issue (or resolve it).
Code looks like
IKernel kernel = new StandardKernel(new NinjectSettings() { LoadExtensions = true });
kernel.Load(Assembly.GetExecutingAssembly());
Program pgm = new Program(kernel.Get<IFSLog>());
Exception is thrown in the last line above with message "Error activating ILog. No matching bindings are available... "
IFSLog is an interface defined in my assembly and its implementation has a dependency on the log4Net Ilog object as below
public class Log4NetLog : IFSLog {
private ILog logger;
public Log4NetLog(ILog log) {
this.logger = log;
}
...
}
The project references the Ninject.extensions.logging.log4net assembly, so my understanding is that the ILog binding should be identified from there.
Also tried the alternate way of specifying bindings manually
public class Bindings : NinjectModule {
public override void Load() {
Bind<IFSLog>().To<Log4NetLog>();
}
}
and initializing kernel as
IKernel kernel = new StandardKernel(new NinjectSettings() { LoadExtensions = false },
new INinjectModule[] {new Bindings(), new Log4NetModule()});
Still same result. Any help much appreciated, thanks in advance...
As it turns out, what Ninject.Extensions.Logging.Log4Net and Ninject.Extensions.Logging.NLog offer is an abstraction of the Log4net and Nlog interfaces, so you can easily swap NLog with Log4Net.
Both extensions only create a binding for
Ninject.Extensions.Logging.ILoggerFactory
andNinject.Extensions.Logging.ILogger
. So that's why there is no binding forILog
.If you inject
ILogger
instead ofILog
into yourLog4NetLog : IFSLog
it will work.Or you can skip using the ninject extensions and wire it up yourself and use the log4net
ILog
interface directly (what are you using theIFSLog
for?).We used to use log4net
ILog
directly and simplified things like this: