gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 00:12:11 27 4
gpt4 key购买 nike

似乎 NLog 不能对 GetCurrentClassLogger() 使用反射,即使我的 MVC 3 应用程序部署在 中完全信任 IIS7 环境。我使用的是 StructureMap 2.6.1,问题似乎在部署之间偶尔出现。我不知道为什么,虽然我不认为 StructureMap 是造成它的原因。
Bootstrapper类(class):

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());
}
}
Registry类(class):
public class MyRegistry : Registry
{
public MyRegistry()//HttpContext context)
{
For<ILogger>().Use<NLogLogger>();
For<IUserRepository>().Use<SqlUserRepository>();
}
}

在我的机器上一切正常。为什么我会收到 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 ) +40
StructureMap.Construction.<>c__DisplayClass2.<CreateBuilder>b__0(IArguments args) +22
StructureMap.Construction.InstanceBuilder.BuildInstance(IArguments args) +12
StructureMap.Pipeline.ConstructorInstance.Build(Type pluginType, BuildSession session, IInstanceBuilder builder) +88

[StructureMapException: StructureMap Exception Code: 207
Internal exception while creating Instance '38000383-149d-45c2-a8f3-7442316da725' of PluginType Todo.Data.ILogger. Check the inner exception for more details.]
StructureMap.Pipeline.ConstructorInstance.Build(Type pluginType, BuildSession session, IInstanceBuilder builder) +276
StructureMap.Pipeline.ConstructorInstance.build(Type pluginType, BuildSession session) +41
StructureMap.Pipeline.SmartInstance_1.build(Type pluginType, BuildSession session) +43
StructureMap.Pipeline.Instance.createRawObject(Type pluginType, BuildSession session) +45
StructureMap.Pipeline.Instance.Build(Type pluginType, BuildSession session) +36
StructureMap.Pipeline.ObjectBuilder.Resolve(Type pluginType, Instance instance, BuildSession session) +103
StructureMap.BuildSession.CreateInstance(Type pluginType, Instance instance) +49
StructureMap.<>c__DisplayClass3.<.ctor>b__1() +22
StructureMap.BuildSession.CreateInstance(Type pluginType) +24
...etc...snipped

最佳答案

事实证明,这不是信任问题,而是 IoC 问题。感谢 NInject solution引导我走向正确的方向。

解决方案

对于您的日志记录包装器,请使用接受字符串参数的构造函数:

public class NLogLogger : ILogger
{
private Logger _logger;

public NLogLogger(string currentClassName)
{
_logger = LogManager.GetLogger(currentClassName);
}
...
}

NLog StructureMap 配置:
For<ILogger>().Use<NLogLogger>()
.Ctor<string>("currentClassName")
.Is(x => x.BuildStack.Root.RequestedType.FullName);

关于structuremap - NLog GetCurrentClassLogger() NullReferenceException 使用 StructureMap(完全信任),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4519261/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com