代码之家  ›  专栏  ›  技术社区  ›  Jose A

ServiceCollection不包含“AddLogging”中的定义

  •  2
  • Jose A  · 技术社区  · 6 年前

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

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

    一定有什么傻事我不见了。目前在ASP.NET核心2.2下,我相信我已经分配了正确的依赖项。

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

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

    我已经重新安装了一个小时了,所以!不能确定问题是什么

    代码如下:

    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

    1 回复  |  直到 6 年前
        1
  •  2
  •   Nkosi    6 年前

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

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

    如图所示,未引用。

    添加对的引用 Microsoft.Extensions.Logging.dll 上述组件。