Reputation: 11099
I'm currently trying to create a Logger so I can inject it in Unit Tests. I'm following https://stackoverflow.com/a/43425633/1057052, and it used to work! I then moved the project and reestablished the dependencies, and now I'm getting
'ServiceCollection' does not contain a definition for 'AddLogging' and no accessible extension method 'AddLogging' accepting a first argument of type 'ServiceCollection' could be found (are you missing a using directive or an assembly reference?)
There must be something silly I'm missing. Currently under ASP.NET Core 2.2, and I believe I have assigned the correct dependencies.
I've been reinstalling for the past hour our so! Can't nail what the problem is
Here's the code:
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Planificacion.UnitTest.Config
{
public class LoggerTestConfig
{
private LoggerTestConfig()
{
}
// https://stackoverflow.com/a/43425633/1057052
public static ILogger<T> GetLoggerConfig<T>() where T : class
{
var serviceProvider = new ServiceCollection()
.AddLogging()
.BuildServiceProvider();
var factory = serviceProvider.GetService<ILoggerFactory>();
return factory.CreateLogger<T>();
}
}
}
Upvotes: 26
Views: 14339
Reputation: 6006
Run this command in powershell
dotnet add package Microsoft.Extensions.Logging.Console
and add this using statement to your code
using Microsoft.Extensions.Logging;
Upvotes: 1
Reputation: 31
If your all configs are correctly but can't use useAutoMapper or smth, update version.
Upvotes: 0
Reputation: 247173
The image highlights that the dependency injection dll is referenced, but desired LoggingServiceCollectionExtensions.AddLogging Method as shown in the links provided, indicates
Namespace: Microsoft.Extensions.DependencyInjection
Assembly: Microsoft.Extensions.Logging.dll <----NOTE THIS
Which is not referenced as shown in the image.
Add a reference to the Microsoft.Extensions.Logging.dll
assembly stated above.
Upvotes: 28