I'm trying to add dependency injection to Nlog using Ninject for unit testing purposes, but I cannot get it to work. :( I'm getting a NullReferenceException when calling GetCurrentClassLogger(). I'm new to both Nlog and Ninject, this is actually my first time using them.
Main
using NLog;
using Ninject;
using Ninject.Extensions.Logging;
using Ninject.Extensions.Logging.NLog2;
public class Program
{
static void Main(string[] args)
{
var kernel = new StandardKernel(new NLogModule());
var logFactory = kernel.Get<ILoggerFactory>();
var run = new MyClass(logFactory);
}
}
MyClass
using Ninject.Extensions.Logging;
public class MyClass
{
private readonly ILogger _log;
public MyClass(ILoggerFactory logFactory)
{
_log = logFactory.GetCurrentClassLogger();
}
public void DoWork()
{
_log.Info("Doing work!");
}
}
Hope someone can help me.