在依赖项注入期间使用NLog

问题描述 投票:0回答:1

我上了这样的课:

   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");
         }
    }

Unity中的课程注册:

container.RegisterType<IABCHelper, ABCHelper>();

当我在某些代码流中调用MakeAsyncCall时,NLog将类名记录为“ DynamicBuildPlanGenerationContext”。

我期望使用“ ABCHelper”而不是“ DynamicBuildPlanGenerationContext”。

我想念什么?

c# unity-container nlog
1个回答
0
投票

首先,如果ABCHelper是您的当前类,这有点值得商,,因为它尚未构建;)

我认为Unity正在使用一些技巧来有效地构建依赖关系。 DynamicBuildPlanGenerationContext的描述>

该对象跟踪生成计划生成的当前状态,累积Microsoft中间语言,提供动态方法的前导和后继,并跟踪生成的MSIL中的局部变量之类的东西,以便可以在MSIL生成策略中重用它们。

NLog尝试通过检查调用栈来查找当前的类名,但可能由于优化而找不到。

我认为,最简单的解决方法是使用命名记录器,例如

public class ABCHelper : ABCBase, IABCHelper
{
    public ABCHelper()
        : base(LogManager.GetLogger(nameof(ABCHelper)))
    {
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.