相关疑难解决方法(0)

使用StructureMap(完全信任)的NLog GetCurrentClassLogger()NullReferenceException

看起来NLog不能使用反射GetCurrentClassLogger(),即使我的MVC 3应用程序部署在IIS7上的完全信任环境中.我正在使用StructureMap 2.6.1,并且问题似乎在部署之间偶尔出现.我无法弄清楚为什么,虽然我认为StructureMap没有引起它.

Bootstrapper 类:

public static class Bootstrapper
{
    public static void ConfigureStructureMap()
    {
        ObjectFactory.Initialize(Init);
    }

    private static void Init(IInitializationExpression x)
    {
        x.AddRegistry(new DBServiceRegistry());
        x.AddRegistry(new MyRegistry());
    }
}
Run Code Online (Sandbox Code Playgroud)

Registry 类:

public class MyRegistry : Registry
{
    public MyRegistry()//HttpContext context)
    {
        For<ILogger>().Use<NLogLogger>();
        For<IUserRepository>().Use<SqlUserRepository>();
    }
}
Run Code Online (Sandbox Code Playgroud)

我的机器上的一切都很棒.System.NullReferenceException: Object reference not set to an instance of an object部署时为什么会出现错误?

[NullReferenceException: Object reference not set to an instance of an object.]
   NLog.LogManager.GetCurrentClassLogger() +84
   lambda_method(Closure , IArguments ) …
Run Code Online (Sandbox Code Playgroud)

structuremap nlog full-trust nullreferenceexception asp.net-mvc-3

12
推荐指数
1
解决办法
6271
查看次数

IIS7中的NLog在发布模式下失败

编辑:正如Kirk Woll正确指出的那样,问题是NLog,而不是NInject.那么让我重新解释一下这个问题:

EDIT2:既然我知道它是NLog + IoC问题,我在ASP.NET MVC2 + Ninject + NLog(+共享主机?)上找到了解决方案= NullReferenceException

我有一个项目正在使用NInject通过ILogger接口将NLogger类注入到我的所有控制器中.Steven,为了回答你的问题以保持我的web.config干净,我使用了一个NLog.Config文件来分离出那个配置.

当我在调试模式下定位IIS7时,代码可以正常工作,但在发布模式下,我得到以下堆栈跟踪.

如果有人知道为什么在调试模式下代码可以工作,为什么在发布它不会,它会非常感激.我还在下面包含了我的NLog配置文件.

<?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>
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪:

[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() …
Run Code Online (Sandbox Code Playgroud)

nlog asp.net-mvc-3

5
推荐指数
1
解决办法
1291
查看次数