在依赖注入期间使用 NLog
Using NLog during dependency injection
我有一个 class 像这样的东西:
public class ABCHelper : ABCBase, IABCHelper
{
public ABCHelper()
: base(LogManager.GetCurrentClassLogger())
{
}
}
public class ABCBase : IABCBase
{
protected readonly Logger logger;
protected ABCBase(Logger logger)
{
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public async Task<HttpResponseMessage> MakeAsyncCall(HttpRequestMessage request)
{
// some code
this.logger.Info("some string");
}
}
Class 在 Unity 中注册:
container.RegisterType<IABCHelper, ABCHelper>();
当我在某些代码流中调用 MakeAsyncCall 时,NLog 将 classname 记录为“DynamicBuildPlanGenerationContext”。
我期待 "ABCHelper" 而不是 "DynamicBuildPlanGenerationContext"。
我错过了什么?
首先,ABCHelper 是否是您当前的 class 有点值得商榷,因为它尚未构建 ;)
我认为 Unity 正在使用一些技巧来有效地构建依赖关系。 DynamicBuildPlanGenerationContext
的描述
This object tracks the current state of the build plan generation, accumulates the Microsoft intermediate language, provides the preamble & postamble for the dynamic method, and tracks things like local variables in the generated MSIL so that they can be reused across MSIL generation strategies.
NLog 尝试通过检查调用堆栈来查找当前 class 名称,但可能由于优化而找不到它。
我认为最简单的解决方法是使用命名记录器,例如通过使用 LogManager.GetLogger(string)
public class ABCHelper : ABCBase, IABCHelper
{
public ABCHelper()
: base(LogManager.GetLogger(nameof(ABCHelper)))
{
}
}
旁注,也许是因为演示,但我在这里没有看到正确注入的记录器。如果您不想将 DI 用于记录器,您可以只创建一个静态字段吗?
如果您确实喜欢使用注入记录器,那么我建议您查看 Microsoft.Extensions.Logging with NLog.Extensions.Logging which also works with Unity and .NET Framework. This could be also useful then:
或者您可以使用
我有一个 class 像这样的东西:
public class ABCHelper : ABCBase, IABCHelper
{
public ABCHelper()
: base(LogManager.GetCurrentClassLogger())
{
}
}
public class ABCBase : IABCBase
{
protected readonly Logger logger;
protected ABCBase(Logger logger)
{
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public async Task<HttpResponseMessage> MakeAsyncCall(HttpRequestMessage request)
{
// some code
this.logger.Info("some string");
}
}
Class 在 Unity 中注册:
container.RegisterType<IABCHelper, ABCHelper>();
当我在某些代码流中调用 MakeAsyncCall 时,NLog 将 classname 记录为“DynamicBuildPlanGenerationContext”。
我期待 "ABCHelper" 而不是 "DynamicBuildPlanGenerationContext"。
我错过了什么?
首先,ABCHelper 是否是您当前的 class 有点值得商榷,因为它尚未构建 ;)
我认为 Unity 正在使用一些技巧来有效地构建依赖关系。 DynamicBuildPlanGenerationContext
This object tracks the current state of the build plan generation, accumulates the Microsoft intermediate language, provides the preamble & postamble for the dynamic method, and tracks things like local variables in the generated MSIL so that they can be reused across MSIL generation strategies.
NLog 尝试通过检查调用堆栈来查找当前 class 名称,但可能由于优化而找不到它。
我认为最简单的解决方法是使用命名记录器,例如通过使用 LogManager.GetLogger(string)
public class ABCHelper : ABCBase, IABCHelper
{
public ABCHelper()
: base(LogManager.GetLogger(nameof(ABCHelper)))
{
}
}
旁注,也许是因为演示,但我在这里没有看到正确注入的记录器。如果您不想将 DI 用于记录器,您可以只创建一个静态字段吗?
如果您确实喜欢使用注入记录器,那么我建议您查看 Microsoft.Extensions.Logging with NLog.Extensions.Logging which also works with Unity and .NET Framework. This could be also useful then:
或者您可以使用