I use Unity, and I register the registrar as follows:
public class MvcApplication : System.Web.HttpApplication { private ILogger _logger; protected void Application_Start() { ... var container = new UnityContainer(); container.RegisterType<ILogger, NLogLogger>(); container.RegisterControllers(); DependencyResolver.SetResolver(new UnityDependencyResolver(container)); _logger = container.Resolve<ILogger>(); _logger.Info("Application started"); }
It seems to be working fine - the message is being logged. Later in global.asax.cs I have the following:
protected void Application_End() { _logger.Info("App is shutting down"); } protected void Application_Error() { Exception lastException = Server.GetLastError(); _logger.Fatal(lastException); }
However, this throws an exception - _logger is null. I suspect I'm doing something wrong with Unity - so what's the right way to use the logger inside global.asax?
source share