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

Viewed 44220
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?

10 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;

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());

Try using ServiceCollection to configure logging in core 3.0

private IServiceCollection ConfigureLogging(IServiceCollection factory)
{
      factory.AddLogging(opt =>
                         {
                               opt.AddConsole();
                         })
      return factory;
}

With .NET Core 3.0 it is quite different to add the console logging. You have to use LoggerFactory.Create() to add this. look microsoft docs here

If you don't have access to the LoggerFactory.Create(), use can still use the ILoggerFactory with the AddProvider() method giving it a ConsoleLoggerProvider() but it is a bit of a pain if you want to do something simple. The problem is, ConsoleLoggerProvider() requires a IOptionsMonitor<ConsoleLoggerOptions> as a parameter and the easiest thing to do if you

  • you don't have access to the options mechanism in your code base (my problem), or
  • the real options mechanisms in your existing code base don't match up with IOptionsMonitor<>, or
  • you have other reasons not to use the ASP.Net options facilities

is to create a dummy class:

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using Microsoft.Extensions.Options;
    
    class DummyConsoleLoggerOptionsMonitor : IOptionsMonitor<ConsoleLoggerOptions>
    {
        private readonly ConsoleLoggerOptions option = new ConsoleLoggerOptions();

        public DummyConsoleLoggerOptionsMonitor(LogLevel level)
        {
            option.LogToStandardErrorThreshold = level;
        }

        public ConsoleLoggerOptions Get(string name)
        {
            return this.option;
        }

        public IDisposable OnChange(Action<ConsoleLoggerOptions, string> listener)
        {
            return new DummyDisposable();
        }

        public ConsoleLoggerOptions CurrentValue => this.option;

        private sealed class DummyDisposable : IDisposable
        {
            public void Dispose()
            {
            }
        }
    }

You can then use your ILoggerFactory like:

factory.AddProvider(
    new ConsoleLoggerProvider(
        new DummyConsoleLoggerOptionsMonitor(LogLevel.Debug)));

I encountered this error in generated code while working with the Uno platform with VS2019. Since the code was generated and the error only occurred after I updated my Nuget packages to their latest versions, it was easy to discover the culprit:

The initial version of Microsoft.Extensions.Logging.Console package was 1.1.1. The latest at this time is 5.0.0. Reverting back to 1.1.1 fixed the issue. It is possible that any version before 5.0.0 might also fix the issue too although I didn't verify.

So downgrading to a suitable version before 5.0.0 for Microsoft.Extensions.Logging.Console should fix the issue.

This was solved for me in .NET Core 5.0 by doing something like this. Example of console with dependency injection

    public static void Main(string[] args)
    {
        //setup our DI
        var serviceProvider = new ServiceCollection()
            .AddLogging(c => c.AddConsole(opt => opt.LogToStandardErrorThreshold = LogLevel.Debug))
            .AddSingleton<IFooService, FooService>()
            .AddSingleton<IBarService, BarService>()
            .BuildServiceProvider();

        var logger = serviceProvider.GetService<ILoggerFactory>()
            .CreateLogger<Program>();
        logger.LogDebug("Starting application");

        //do the actual work here
        var bar = serviceProvider.GetService<IBarService>();
        bar.DoSomeRealWork();

        logger.LogDebug("All done!");
    }

I had same problem with my app and I had to add

<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.0"/>

to my .csproj file and rebuild the solution. It fixed problem in my case.

I had the same problem but could not installed the extension even if I add the package reference on my .csproj instead I just went to the nuget site of Microsoft.Extensions.Logging.Console and use this command on Package Manager Console as dotnet add package Microsoft.Extensions.Logging.Console doesnt work for me.

Install-Package Microsoft.Extensions.Logging.Console -Version 3.1.5

If you have a

private static ILoggerFactory LoggerFactory { get; set; }

Instead of

private static LoggerFactory LoggerFactory { get; set; }

You will get the error due to the injection dependency from the previous version of dotnet.

Related