title: Resolving Ninject ILogger in Global.asax
body: <p>I'm trying to use the dependency resolver in the <em>global.asax</em> of an MVC4 project to get a reference to <em>Ninject.Extensions.Logging.ILogger</em>, but it's throwing a <em>NullReferenceException</em>. I'm using constructor injection elsewhere in the project to get instances of ILogger, and that works flawlessly.</p>
  
  <pre><code>protected void Application_EndRequest()
  {
      if (Context.Response.StatusCode == 404)
      {
          Response.Clear();
  
          var rd = new RouteData();
          rd.Values["controller"] = "Error";
          rd.Values["action"] = "NotFound";
  
          //this line throws a NullReferenceException with the stack track below
          var logger = DependencyResolver.Current.GetService&lt;ILogger&gt;();
  
          IController c = new ErrorController(logger);
          c.Execute(new RequestContext(new HttpContextWrapper(Context), rd));
      }
  }
  
  at Ninject.Extensions.Logging.LoggerFactoryBase.GetLogger(IContext context) in c:\Projects\Ninject\ninject.extensions.logging\src\Ninject.Extensions.Logging\LoggerFactoryBase.cs:line 61
  at Ninject.Extensions.Logging.LoggerModuleBase.&lt;Load&gt;b__0(IContext context) in c:\Projects\Ninject\ninject.extensions.logging\src\Ninject.Extensions.Logging\LoggerModuleBase.cs:line 26
  at Ninject.Activation.Providers.CallbackProvider`1.CreateInstance(IContext context) in c:\Projects\Ninject\ninject\src\Ninject\Activation\Providers\CallbackProvider.cs:line 45
  at Ninject.Activation.Provider`1.Create(IContext context) in c:\Projects\Ninject\ninject\src\Ninject\Activation\Provider.cs:line 38
  at Ninject.Activation.Context.Resolve() in c:\Projects\Ninject\ninject\src\Ninject\Activation\Context.cs:line 157
  at Ninject.KernelBase.&lt;&gt;c__DisplayClass10.&lt;Resolve&gt;b__c(IBinding binding) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:line 386
  at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
  at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
  at Ninject.Web.Mvc.NinjectDependencyResolver.GetService(Type serviceType) in c:\Projects\Ninject\ninject.web.mvc\mvc3\src\Ninject.Web.Mvc\NinjectDependencyResolver.cs:line 56
  at System.Web.Mvc.DependencyResolverExtensions.GetService[TService](IDependencyResolver resolver)
  at My.WebApp.MvcApplication.Application_EndRequest() in c:\Dev\Project\My.WebApp\Global.asax.cs:line 92
  </code></pre>
  
  <p>If I replace the DependencyResolver line with the following, it works, but is it ok to create multiple Kernel instances?</p>
  
  <pre><code>var kernel = new StandardKernel();
  ILoggerFactory loggerFactory = kernel.Get&lt;ILoggerFactory&gt;();
  var logger = loggerFactory.GetCurrentClassLogger();
  </code></pre>