NLog 适用于 ASP.NET Core App 但不适用于 .NET Core xUnit 测试项目 [英] NLog works in ASP.NET Core App but not in .NET Core xUnit Test Project

查看:32
本文介绍了NLog 适用于 ASP.NET Core App 但不适用于 .NET Core xUnit 测试项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 .NET Core 类库,它定义了实用程序类接口,并实现了每个接口,例如通过 ADO.NET 访问数据库等.

I've created a .NET Core class library that defines utility class interfaces and that implements each interface for things like database access via ADO.NET, etc.

每个实现类都使用 NLog.这在包含和引用 .NET Core 类库的 ASP.NET Core Web 应用程序中非常有效.

Each implementation class utilizes NLog. This works great in an ASP.NET Core Web application that includes and references the .NET Core class library.

但是,当我尝试在 .NET Core xUnit Test 项目的上下文中实例化这些相同的实现类时,我得到

However, when I try to instantiate these same implementation classes in the context of a .NET Core xUnit Test project, I get

无法将NLog.Logger"类型的对象转换为Microsoft.Extensions.Logging.ILogger"类型

Unable to cast object of type 'NLog.Logger' to type 'Microsoft.Extensions.Logging.ILogger'

我已经搜索过类似的问题,但没有找到.

I've searched for similar issues but found none.

在每个类中,我都声明了一个私有的只读变量:

In each class I've declared a private readonly variable:

private readonly ILogger<DatabasePersistenceAdoDotNet> _logger = null;

我像这样实例化这个变量:

And I'm instantiating this variable like this:

_logger = (ILogger<DatabasePersistenceAdoDotNet>)NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();

这适用于包含并引用 .NET Core 类库的 ASP.NET Core Web 应用程序,但在我的 .NET Core xUnit Test 项目中失败了.

This works in an ASP.NET Core Web application that includes and references the .NET Core class library but blows up in my .NET Core xUnit Test project.

我怀疑这与 ASP.NET 的依赖注入在 .NET Core xUnit Test 项目中不可用有关,但我不知道如何让我的实用程序包在这两种情况下都能正常工作.

I suspect that this has something to do with ASP.NET's dependency inject not being available in the .NET Core xUnit Test project, but I can't figure out how to make my utility package work correctly in both contexts.

推荐答案

在 ASP.NET Core 中使用 NLog 时,有两种样式:

When using NLog with ASP.NET Core, there are two styles:

  1. 非 DI 样式,使用 GetCurrentClassLogger()
  2. DI 风格,需要 NLog.Extensions.Logging/NLog.Web.AspNetCore 包在 NLog 和 Microsoft.Extensions.Logging 之间架起桥梁.

当使用 .GetCurrentClassLogger() 时,您将获得一个 NLog 记录器对象(它实现了 NLog.ILogger,而不是 Microsoft.Extensions.Logging.ILogger).

When using .GetCurrentClassLogger(), you wil get a NLog logger object (which implements NLog.ILogger, not Microsoft.Extensions.Logging.ILogger).

因此,如果您需要 Microsoft.Extensions.Logging.ILogger,则不能使用 .GetCurrentClassLogger().您需要:

So if you need a Microsoft.Extensions.Logging.ILogger, you can't use the .GetCurrentClassLogger(). You need to:

  1. 自己设置整个DI系统并构造函数注入Microsoft.Extensions.Logging.ILogger,或
  2. 使用 NLog.Extensions.Logging 包的类.

选项 2 是一种简单的方法,我将向您展示方法:

Option 2 is the easy way and I will show you how:

// Load NLog
NLog.Web.NLogBuilder.ConfigureNLog("nlog.config");

// Create provider to bridge Microsoft.Extensions.Logging
var provider = new NLog.Extensions.Logging.NLogLoggerProvider();

// Create logger
Microsoft.Extensions.Logging.ILogger logger = provider.CreateLogger(typeof(MyClass).FullName);

请注意,您无法通过这种方式创建 Microsoft.Extensions.Logging.ILogger!只是一个非通用的 Microsoft.Extensions.Logging.ILogger.如果您需要通用版本,那么您需要 DI 设置,这将处理两者之间的转换.

Please note, you can't create a Microsoft.Extensions.Logging.ILogger<MyClass> this way! Just a non-generic Microsoft.Extensions.Logging.ILogger. If you need the generic version, then you need the DI setup and that will handle the conversion between both.

也很高兴知道,在测试项目中找到 nlog.config 很难,因为单元测试框架会移动二进制文件等.我建议使用配置 API

Also good to know, finding a nlog.config in a test project is hard, as unit test frameworks will move the binaries etc. I would recommend to use the config API

例如

var configuration = new LoggingConfiguration();
configuration.AddRuleForAllLevels(new ConsoleTarget());
LogManager.Configuration = configuration;

var configuration = XmlLoggingConfiguration.CreateFromXmlString("<nlog>....</nlog>"); //full nlog.config as string
LogManager.Configuration = configuration;

这篇关于NLog 适用于 ASP.NET Core App 但不适用于 .NET Core xUnit 测试项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆