Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 633
Drago Avatar asked Oct 06 '19 17:10

Drago


People also ask

Is it possible to add a console in the iloggerfactory?

'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.

Does'iloggerfactory'contain a definition for'addconsole (iloggingbuilder)'?

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.

What happened to addconsole () method in ILogger?

‘ConsoleLoggerExtensions.AddConsole (ILoggerFactory)’ is obsolete: ‘This method is obsolete and will be removed in a future version. The recommended alternative is AddConsole (this ILoggingBuilder builder).’ ‘ConsoleLoggerExtensions.AddDebug (ILoggerFactory)’ is obsolete: ‘This method is obsolete and will be removed in a future version.

How to add 'addlog4net' to 'iloggerfactory'?

‘ILoggerFactory’ does not contain a definition for ‘AddLog4Net’ and no accessible extension method ‘AddLog4Net’ accepting a first argument of type ‘ILoggerFactory’ could be found (are you missing a using directive or an assembly reference?) Run this NUGET Command from the Package Manager Console in Visual Studio.


2 Answers

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; 
like image 56
crumdev Avatar answered Sep 20 '22 08:09

crumdev


There is a seperate issue at play, previously the signature for AddConsole() expected an ILoggerFactory, that has since changed to an ILoggerBuilder, as hinted at in the error message.

The following it seems is the new way to stand up a new Console logger:

var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());

like image 26
Brendan Grant Avatar answered Sep 17 '22 08:09

Brendan Grant