netcore 2.1:无法使用新的LoggerFactory()创建记录器

时间:2018-08-29 08:55:53

标签: logging .net-core

我正在将一个netcore 2.0项目升级到netcore2.1

我有一个创建记录器的基本测试控制器,如下所示:

public class WSTestController
{
    protected readonly ILogger _log;
    protected readonly HttpClient _client;
    protected readonly IServiceCollection _services;

    public WSTestController() {
        _log = new LoggerFactory().AddConsole().CreateLogger(this.GetType().Name);
        var testContext = new TestContext();
        _client = testContext.Client;
    }

这在netcore 2.0上正常工作,但是在netcore 2.1中,出现以下错误:

controllers/WSTestController.cs(22,40): error CS1061: 
'LoggerFactory' does not contain a definition for 'AddConsole' and no accessible extension method 'AddConsole' accepting a first argument of type 'LoggerFactory' could be found 
(are you missing a using directive or an assembly reference?) [[...]/src/mtss-ws.integrationtests/mtss-ws.integrationtests.csproj]

如何在netvore 2.1中手动实例化控制台记录器(没有DI)?

2 个答案:

答案 0 :(得分:1)

它仍然可以在netcore2.1中运行,只是看起来here

您还可以通过简单地使用AddProvider方法来手动执行此操作:

loggerFactory.AddProvider(
    new ConsoleLoggerProvider(
        (text, logLevel) => logLevel >= LogLevel.Debug, true));

答案 1 :(得分:0)

此错误与我从netcore 2.0升级到2.1有关

在此SO question中我对此进行了解释,但基本上我必须在测试项目中添加对Microsoft.AspNetCore.App的程序包引用,如此migration guide

中所述