我正在将netcore 2.0项目升级到netcore2.1
我有一个基础测试控制器,可以创建一个 Logger ,如下所示:
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中手动实例化控制台 Logger (即没有DI)?
2 回答
它应该仍然可以在netcore2.1中工作,只需看看here .
您也可以通过简单地使用AddProvider方法手动执行此操作:
此错误与我从netcore 2.0升级到2.1有关
在这个SO question我解释一下,但基本上我不得不在测试项目中添加一个对Microsoft.AspNetCore.App的包引用,如migration guide中所述