24

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

Pang
  • 9,564
  • 146
  • 81
  • 122
Jose A
  • 10,053
  • 11
  • 75
  • 108
  • You should [not use a DI Container when writing unit tests](https://stackoverflow.com/questions/32594803/using-di-container-in-unit-tests). – Steven Jan 23 '19 at 09:00
  • @Steven: Thanks! I wasn't. I was trying to create the ILogger concrete implementation so I could inject it in the dependencies that needed it. – Jose A Jan 23 '19 at 20:31
  • The ServiceCollection *is* a container. You should create the class under tedt by hand by injecting a fake logger manually. Not by a container. – Steven Jan 23 '19 at 20:45
  • @Steven: Does that mean that I'm doing integration tests instead of true unit tests? – Jose A Jan 23 '19 at 20:52
  • That is a question I can't answer. An integration test is meant to test a larger part of the application, typically in relation with some of its external components, while a unit test is meant to test an isolated 'unit' (or use case). I don't know which you are trying to achieve. – Steven Jan 23 '19 at 21:26
  • @Steven I was trying to achieve a Unit Test mocking as little as possible. But I guess that's where Integration Tests fall into. – Jose A Jan 23 '19 at 22:05
  • 1
    Ok, before Stack Overflow complaints. I've found my answer in [here ](https://softwareengineering.stackexchange.com/questions/65477/why-is-it-important-that-a-unit-test-not-test-dependencies?rq=1) – Jose A Jan 23 '19 at 23:54

1 Answers1

25

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.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • ! I have learned something super important today! That was it! – Jose A Jan 22 '19 at 23:03
  • I have the nuget package Microsoft.Extensions.Logging, I declare a using in the file, but still it refuses to compile in .Net Core 2.1. I will try with .Net Core 2.2 SDK – Vilmir Jun 06 '19 at 11:13
  • @vilmir maybe this is three years too late but a common mistake is putting AddLogging against the wrong type. For example .AddLogging() would not compile. .AddLogging() would compile. – Jan David Narkiewicz Sep 23 '22 at 01:08