'ILoggerFactory' does not contain a definition for 'AddConsole'
Solution 1
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;
Solution 2
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());
Solution 3
Try using ServiceCollection to configure logging in core 3.0
private IServiceCollection ConfigureLogging(IServiceCollection factory)
{
factory.AddLogging(opt =>
{
opt.AddConsole();
})
return factory;
}
Solution 4
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
Solution 5
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)));
Drago
Updated on July 14, 2022Comments
-
Drago almost 2 years
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 methodAddConsole()
? How can I fix this? -
Ponni Radhakrishnan over 4 yearsThis does not work even the example provided from MS doesn't work
-
CLoc over 4 yearsdo 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
-
Ponni Radhakrishnan over 4 yearsThe example from the Microsoft docs you quoted directly will list AddConsole() as not found
-
Willem Ellis over 3 yearsIf you are getting
AddConsole()
not found as mentioned above, add a reference toMicrosoft.Extensions.Logging.Console
to your csproj and it will work. -
Rei Miyasaka over 2 yearsI hate extension methods.
-
Beltway over 2 yearsIf 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.