private ILoggerFactory ConfigureLogging(ILoggerFactory factory)
{
factory.AddConsole();
return factory;
}
我在Github上找到了上面的代码。它给出以下错误:
'ILoggerFactory' does not contain a definition for 'AddConsole' and the best extension method overload 'ConsoleLoggerExtensions.AddConsole(ILoggingBuilder)' requires a receiver of type 'ILoggingBuilder'
我正在使用NET Core 3.0,并且安装了以下NuGet软件包。
<PackageReference Include="Discord.Net" Version="2.1.1" />
<PackageReference Include="Discord.Net.Commands" Version="2.1.1" />
<PackageReference Include="Discord.Net.WebSocket" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.0.0" />
为什么我得到
ILoggerFactory
没有方法AddConsole()
?我怎样才能解决这个问题? 最佳答案
我只是在学习Pluralsight的类(class)后才遇到这个问题。在下一张幻灯片之前,我先介绍了自己的.AddConsole为什么在ILoggerFactory.Create中起作用的原因。
即使您的类只需要using Microsoft.Extensions.Logging
,也需要显式向.Net Core应用程序添加程序包引用,以便找到.AddConsole方法。
dotnet add package Microsoft.Extensions.Logging.Console
并将此using语句添加到您的代码中
using Microsoft.Extensions.Logging;