Using the Ninject NLogModule Logger in global.asax

Posted by Ciaran on Stack Overflow See other posts from Stack Overflow or by Ciaran
Published on 2009-10-07T15:35:13Z Indexed on 2010/05/10 23:04 UTC
Read the original article Hit count: 634

Filed under:
|
|

I'm using Ninject for DI in my asp.net application, so my Global class inherits from NinjectHttpApplication.

In my CreateKernel(), I'm creating my custom modules and DI is working fine for me.

However, I notice that there is a Logger property in the NinjectHttpApplication class, so I'm trying to use this in Application_Error whenever an exception is not caught.

I think I'm creating the nlog module correctly for Ninject, but my logger is always null. Here's my CreateKernel:

protected override Ninject.Core.IKernel CreateKernel()
{
    IKernel kernel = new StandardKernel(new NLogModule(), new NinjectRepositoryModule());
    return kernel;
}

But in the following method, Logger is always null.

protected void Application_Error(object sender, EventArgs e) 
{
    Exception lastException = Server.GetLastError().GetBaseException();
    Logger.Error(lastException, "An exception was caught in Global.asax");
}

To clarify, Logger is a property on NinjectHttpApplication of type ILogger and has the [Inject] attribute

Any idea how to inject correctly into Logger?

© Stack Overflow or respective owner

Related posts about ninject

Related posts about global-asax