Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NLog Failing on Release Mode in IIS7

EDIT: As Kirk Woll correctly pointed out, the issue is NLog, not with NInject. So let me rephrase the issue:

EDIT2: Now that I knew it was NLog + an IoC problem, I found the solution at ASP.NET MVC2 + Ninject + NLog (+ shared hosting?) = NullReferenceException

I have a project that is using NInject to inject an NLogger class via an ILogger interface into all of my controllers. Steven, to answer your question to keep my web.config clean, I have used an NLog.Config file to separate out that configuration.

When I target IIS7 in Debug mode, the code works correctly, but in Release Mode I get the below stack trace.

If anyone has any idea why in Debug Mode the code would work, and why in Release it wouldn't, it'd be much appreciated. I've also included my NLog Config File below.

<?xml version="1.0"?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets>
    <target name="console" xsi:type="ColoredConsole" 
            layout="${date:format=HH\:mm\:ss}|${level}|${stacktrace}|${message}" />
    <target name="file" xsi:type="File" fileName="${basedir}/App_Data//Logs/site.log" 
            layout="${date}: ${message}" />
    <target name="eventlog" xsi:type="EventLog" 
            source="Template" 
            log="Application"
            layout="${date}: ${message} ${stacktrace}" />
  </targets>
  <rules>
    <logger name="*" minlevel="Info" writeTo="file" />
    <logger name="*" minlevel="Fatal" writeTo="eventlog" />        
  </rules>
</nlog>

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   NLog.LogManager.GetCurrentClassLogger() +84
   DynamicInjectorc5f536e7a4564738b2e779e62f9c20c7(Object[] ) +40
   Ninject.Activation.Providers.StandardProvider.Create(IContext context) in c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Activation\Providers\StandardProvider.cs:81
   Ninject.Activation.Context.Resolve() in c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Activation\Context.cs:157
   Ninject.KernelBase.<Resolve>b__7(IContext context) in c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\KernelBase.cs:375
   System.Linq.<>c__DisplayClass12`3.<CombineSelectors>b__11(TSource x) +32
   System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +151
   System.Linq.Enumerable.SingleOrDefault(IEnumerable`1 source) +4222965
   Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent) in c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Planning\Targets\Target.cs:179
   Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent) in c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Planning\Targets\Target.cs:147
   Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target) in c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Activation\Providers\StandardProvider.cs:97
   Ninject.Activation.Providers.<>c__DisplayClass2.<Create>b__1(ITarget target) in c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Activation\Providers\StandardProvider.cs:81
   System.Linq.WhereSelectArrayIterator`2.MoveNext() +85
   System.Linq.Buffer`1..ctor(IEnumerable`1 source) +217
   System.Linq.Enumerable.ToArray(IEnumerable`1 source) +78
   Ninject.Activation.Providers.StandardProvider.Create(IContext context) in c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Activation\Providers\StandardProvider.cs:81
   Ninject.Activation.Context.Resolve() in c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Activation\Context.cs:157
   Ninject.KernelBase.<Resolve>b__7(IContext context) in c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\KernelBase.cs:375
   System.Linq.<>c__DisplayClass12`3.<CombineSelectors>b__11(TSource x) +32
   System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +151
   System.Linq.Enumerable.SingleOrDefault(IEnumerable`1 source) +4222965
   Ninject.Web.Mvc.NinjectDependencyResolver.GetService(Type serviceType) in c:\Projects\Ninject\Maintenance2.2\ninject.web.mvc\mvc3\src\Ninject.Web.Mvc\NinjectDependencyResolver.cs:56
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +51

[InvalidOperationException: An error occurred when trying to create a controller of type 'BossP.Controllers.HomeController'. Make sure that the controller has a parameterless public constructor.]
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +182
   System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +80
   System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +74
   System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +232
   System.Web.Mvc.<>c__DisplayClass6.<BeginProcessRequest>b__2() +49
   System.Web.Mvc.<>c__DisplayClassb`1.<ProcessInApplicationTrust>b__a() +13
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Func`1 func) +124
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +98
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +50
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8963444
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
like image 631
Andrew Avatar asked Nov 04 '22 11:11

Andrew


2 Answers

Well, I spend 2 nights on investigation and solving the issue. Initially I thought that problem related to NLog logging framework so I switched to Log4Net instead. That was easy because I used Ninject.Extensions.Logging. But that didn't helped.

Then I updated to prerelease version of Ninject and exception message became more clear: "Can not determine current class for DynamicInjectorb709a37f02d44e5a8615c5fde4e0746d".

Finally some googling pointed me to the following discussion: https://groups.google.com/forum/#!msg/ninject/V_bS0ykJxAA/IBnsGVnL50UJ

Solution: "Try use reflection instead by creating a kernel with a NinjectSettings instance that has UseReflectionBasedInjection set to true"

like image 50
Yevgen Safronov Avatar answered Nov 09 '22 15:11

Yevgen Safronov


I have experienced a similar problem.

My (simplistic) solution:

replace these lines

private Logger Logger = LogManager.GetCurrentClassLogger();

by this line

private static Logger Logger = LogManager.GetCurrentClassLogger();

The "readonly" keyword is optional.

like image 37
Maxim Eliseev Avatar answered Nov 09 '22 14:11

Maxim Eliseev