gpt4 book ai didi

c# - ILoggerFactory.GetCurrentClassLogger 仅在发布版本中因 InvalidOperationException 而失败

转载 作者:行者123 更新时间:2023-11-30 15:23:43 24 4
gpt4 key购买 nike

上周将我的 Windows 服务应用程序部署到生产环境失败,所以我尝试在本地以 Release模式运行该项目并得到 InvalidOperationException: Can not determine current class. 这可以追溯到调用GetCurrentClassLogger

我的项目使用 NinjectILoggerFactory 解析为中间层的每个服务。然后服务使用 GetCurrentClassLogger() 在构造函数中获取正确的 ILogger。例如:

public class FooService : IFooService
{
private readonly ILogger logger;

public FooService(ILoggerFactory loggerFactory)
{
this.logger = loggerFactory.GetCurrentClassLogger();
}

// ...
}

如果我在 Ninject.Extensions.LoggingLoggerFactoryBase 深入研究 GetCurrentClassLogger 的实现:

[MethodImpl(MethodImplOptions.NoInlining)]
public ILogger GetCurrentClassLogger()
{
StackFrame stackFrame = new StackFrame(0, false);
if (stackFrame.GetMethod() == (MethodBase) LoggerFactoryBase.getCurrentClassLoggerMethodInfo)
stackFrame = new StackFrame(1, false);
Type declaringType = stackFrame.GetMethod().DeclaringType;
if (declaringType == (Type) null)
throw new InvalidOperationException(string.Format("Can not determine current class. Method: {0}", (object) stackFrame.GetMethod()));
return this.GetLogger(declaringType);
}

我可以看到抛出异常的地方。

我的第一直觉是检查是否有任何系统、框架或项目更新导致它,但自上次成功部署以来没有做任何有意义的事情。

现在...这个问题的“有趣”之处在于,当我在构造函数中添加一行 Trace.WriteLine("Test"); 时,GetCurrentClassLogger 执行得很好。

我能够将这个问题与编译器的代码优化联系起来。如果我在项目属性的 Build 选项卡中禁用它,它也可以正常执行。

问题:什么会导致 StackFrame 停止提供 DeclaringType

与此同时,我正在使用它作为解决方法,但我更愿意使用原始方法:

this.logger = loggerFactory.GetLogger(this.GetType());

如有任何帮助,我们将不胜感激。

最佳答案

在不查看发出的 CIL 的情况下,很难确切地说出行为不同的原因,但我猜它正在内联。如果您将构造函数属性化为:

[MethodImpl(MethodImplOptions.NoInlining)]

就像 NInject 代码所做的那样。该方法可能会被内联,因为它只是分配了一个只读字段的一行。您可以在此处了解内联如何影响 StackFrames:http://www.hanselman.com/blog/ReleaseISNOTDebug64bitOptimizationsAndCMethodInliningInReleaseBuildCallStacks.aspx

就是说,这种不可预测性是使用堆栈帧不是一个好主意的原因。如果您想要不必指定类名的语法,正在使用 VS 2012,并且您的文件名与您的类名匹配,您可以在扩展方法上使用 CallerFilePath 属性并保留与现在相同的行为,而无需使用栈帧。看: https://msdn.microsoft.com/en-us/library/hh534540.aspx

更新:

另一种方法可能是直接注入(inject) ILogger 并让您的容器负责确定要注入(inject)的组件的类型(因为它肯定知道)。这看起来也更干净。请参阅此处的示例:https://github.com/ninject/ninject.extensions.logging/issues/19

    this.Bind<ILogger>().ToMethod(context =>
{
var typeForLogger = context.Request.Target != null
? context.Request.Target.Member.DeclaringType
: context.Request.Service;
return context.Kernel.Get<ILoggerFactory>().GetLogger(typeForLogger);
});

关于c# - ILoggerFactory.GetCurrentClassLogger 仅在发布版本中因 InvalidOperationException 而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34146393/

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