'ILoggerFactory' does not contain a definition for 'AddConsole'

Drago picture Drago · Oct 6, 2019 · Viewed 17.8k times · Source
private ILoggerFactory ConfigureLogging(ILoggerFactory factory)
{
      factory.AddConsole();
      return factory;
}

I have found the piece of code above on Github. It gives the following error:

'ILoggerFactory' does not contain a definition for 'AddConsole' and the best extension method overload 'ConsoleLoggerExtensions.AddConsole(ILoggingBuilder)' requires a receiver of type 'ILoggingBuilder'

I'm using NET Core 3.0 and I have the following NuGet packages installed.

<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" />

Why do I get that ILoggerFactory does not have the method AddConsole()? How can I fix this?

Answer

crumdev picture crumdev · Jan 18, 2020

I just ran into this following a course on Pluralsight. I got ahead of myself before the next slide explaining why their .AddConsole was working in the ILoggerFactory.Create.

Even though you only need using Microsoft.Extensions.Logging in your class, you need to explicitly add a package reference to your .Net Core app in order for the .AddConsole method to be found.

dotnet add package Microsoft.Extensions.Logging.Console

and add this using statement to your code

using Microsoft.Extensions.Logging;