Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ServiceCollection does not contain a definition from "AddLogging"

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.

https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection?view=aspnetcore-2.2

https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.loggingservicecollectionextensions.addlogging?view=aspnetcore-2.2

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

    }
}

enter image description here

like image 698
Jose A Avatar asked Jan 22 '19 21:01

Jose A


1 Answers

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.

like image 91
Nkosi Avatar answered Nov 12 '22 03:11

Nkosi