Search code examples
c#ninjectnlogninject-extensions

How to get Logger in void Main with Ninject.Extensions.Logging?


I'm using Ninject for my DI and Ninject.Extensions.Logging (with NLog) for Logger injection. I want to get an instance of Logger in my Main void at the start of the program.

I tried this code:

private static void Main()
{
    var logger = new StandardKernel(new NLogModule()).Get<ILogger>();
}

But I get an error Error loading module 'Ninject.Extensions.Logging.NLog2.NLogModule' of type NLogModule.

How can I get a Logger instance in this method?


Solution

  • Extensions are loaded automatically. You cant load it again manually. Just use

    new StandardKernel().Get<ILoggerFactory>().GetCurrentClassLogger();