ServiceCollection 不包含“AddLogging”中的定义

问题描述 投票:0回答:2

我目前正在尝试创建一个记录器,以便可以将其注入单元测试中。我正在关注 https://stackoverflow.com/a/43425633/1057052,它曾经有效!然后我移动了项目并重新建立了依赖关系,现在我得到了

“ServiceCollection”不包含“AddLogging”的定义并且 没有可访问的扩展方法“AddLogging”接受第一个参数 可以找到“ServiceCollection”类型(您是否缺少使用 指令还是程序集参考?)

我一定错过了一些愚蠢的东西。目前在 ASP.NET Core 2.2 下,我相信我已经分配了正确的依赖项。

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

过去一个小时我一直在重新安装我们的so!无法确定问题出在哪里

这是代码:

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

    }
}

c# unit-testing logging asp.net-core dependency-injection
2个回答
26
投票

该图像突出显示依赖项注入 dll 已被引用,但需要 LoggingServiceCollectionExtensions.AddLogging Method,如提供的链接所示,指示

Namespace:    Microsoft.Extensions.DependencyInjection
Assembly:    Microsoft.Extensions.Logging.dll <----NOTE THIS

如图所示,未引用。

添加对上述

Microsoft.Extensions.Logging.dll
程序集的引用。


0
投票

如果你的所有配置都正确但无法使用useAutoMapper或smth,请更新版本。

© www.soinside.com 2019 - 2024. All rights reserved.