90
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?

jps
  • 20,041
  • 15
  • 75
  • 79
Drago
  • 1,755
  • 3
  • 19
  • 30
  • Possible duplicate of [How to fix obsolete ILoggerFactory methods?](https://stackoverflow.com/questions/53840298/how-to-fix-obsolete-iloggerfactory-methods) – golobitch Oct 06 '19 at 17:28
  • My question may be stupid, but did you check that the `AddConsole` method definition is included in what you declared with `using` statements? – Romain Reboulleau Oct 06 '19 at 17:28
  • For NET Core 5, I had this problem because I neglected to add the right package. `Install-Package Microsoft.Extensions.Logging.Console` solved it for me. – patrickmdnet Oct 28 '21 at 15:47

11 Answers11

131

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;
granadaCoder
  • 26,328
  • 10
  • 113
  • 146
crumdev
  • 1,599
  • 2
  • 11
  • 13
  • 10
    I hate extension methods. – Rei Miyasaka Sep 13 '21 at 21:12
  • 2
    If my Intellisense has not gone completely brain-dead this necessity appears to be absolutely arbitrary on minor releases. I am looking at two separate projects referencing 5.0.8 and 5.0.12 respectively and the former requires it while the latter does not. – Beltway Nov 10 '21 at 15:42
  • consider also package Microsoft.Extensions.Logging.Debug if you, like me, found this answer when looking for missing .AddDebug() – Ekus Apr 13 '23 at 13:57
39

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

Brendan Grant
  • 937
  • 7
  • 16
29

Try using ServiceCollection to configure logging in core 3.0

private IServiceCollection ConfigureLogging(IServiceCollection factory)
{
      factory.AddLogging(opt =>
                         {
                               opt.AddConsole();
                         })
      return factory;
}
William Magno
  • 502
  • 5
  • 6
8

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

Kot
  • 445
  • 4
  • 9
CLoc
  • 190
  • 1
  • 7
  • 3
    This does not work even the example provided from MS doesn't work – Charles Okwuagwu Nov 16 '19 at 18:01
  • 1
    do you get an error message in the example, or what doesn't work for you? Some information wouldn't be bad, so we can help you instead of distributing a downvote directly – CLoc Nov 26 '19 at 09:17
  • 1
    The example from the Microsoft docs you quoted directly will list AddConsole() as not found – Charles Okwuagwu Nov 26 '19 at 09:37
  • 5
    If you are getting `AddConsole()` not found as mentioned above, add a reference to `Microsoft.Extensions.Logging.Console` to your csproj and it will work. – Willem Ellis Oct 15 '20 at 14:40
7

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!");
    }
Sabilace
  • 406
  • 5
  • 5
4

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)));
mheyman
  • 4,211
  • 37
  • 34
4

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.

Chuma
  • 714
  • 3
  • 7
2

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.

kateww
  • 21
  • 1
2

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
Crismogram
  • 906
  • 15
  • 27
0

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.

0

This worked for me, based on How to create a LoggerFactory with a ConsoleLoggerProvider?

        var configureNamedOptions = new ConfigureNamedOptions<ConsoleLoggerOptions>("", null);
        var optionsFactory = new OptionsFactory<ConsoleLoggerOptions>(new[] { configureNamedOptions }, Enumerable.Empty<IPostConfigureOptions<ConsoleLoggerOptions>>());
        var optionsMonitor = new OptionsMonitor<ConsoleLoggerOptions>(optionsFactory, Enumerable.Empty<IOptionsChangeTokenSource<ConsoleLoggerOptions>>(), new OptionsCache<ConsoleLoggerOptions>());
        loggerFactory.AddProvider(new ConsoleLoggerProvider(optionsMonitor));
Andy Joiner
  • 5,932
  • 3
  • 45
  • 72