简体   繁体   English

如何使用 DI 注册 ILogger(Microsoft.Extensions.Logging) 使用 autofac .net 框架

[英]How to Register ILogger(Microsoft.Extensions.Logging) with DI usinq autofac .net framework

I use ILogger from Microsoft.Extensions.Logging in a .net framework project.我在 .net 框架项目中使用 Microsoft.Extensions.Logging 的 ILogger。 Now I want to register the ILogger in the container but i cant.All the answers are about .net core.现在我想在容器中注册 ILogger 但我不能。所有的答案都是关于 .net 核心。 i try我试试

var builder = new ContainerBuilder();

builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

//first try
builder.RegisterGeneric(typeof(Logger<>)).As(typeof(ILogger<>));
IServiceCollection services = new ServiceCollection();

//second try
services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
IContainer container = builder.Build();

httpConfig.DependencyResolver = new AutofacWebApiDependencyResolver(container);

also my class is我的 class 也是

public class TestController : ApiController
{
    private readonly ILogger<TestController > _logger;
    private readonly IService _service;

    public TestController (IService service, ILogger<TestController > logger)
        {
            _service = service;
            _logger = logger;
        }
}

The di is correct because other services have injected correct. di 是正确的,因为其他服务已正确注入。 When i include the logger in constructor i get the message An error occurred when trying to create a controller of type 'TestController '.当我在构造函数中包含记录器时,我收到消息尝试创建“TestController”类型的 controller 时发生错误。 Make sure that the controller has a parameterless public constructor.确保 controller 具有无参数的公共构造函数。

Since trying to integraten with those extensions, consider populating the service collection is expected and populating the builder once everything is registered,由于尝试与这些扩展集成,请考虑填充服务集合并在注册所有内容后填充构建器,

Example from docs来自文档的示例

var builder = new ContainerBuilder();

builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

IServiceCollection services = new ServiceCollection();

// The Microsoft.Extensions.Logging package provides this one-liner
// to add logging services.
services.AddLogging();

// Once you've registered everything in the ServiceCollection, call
// Populate to bring those registrations into Autofac. This is
// just like a foreach over the list of things in the collection
// to add them to Autofac.
builder.Populate(services);

IContainer container = builder.Build();

httpConfig.DependencyResolver = new AutofacWebApiDependencyResolver(container);

Reference Autofac: .Net Core Integration参考Autofac:.Net Core 集成

ILogger is great in.Net core, but in.Net framework, I'd suggest using ILog (log4net). ILogger 在.Net 核心中很棒,但在.Net 框架中,我建议使用 ILog (log4net)。

Implement log4net in a windows service using dependency injection 使用依赖注入在 windows 服务中实现 log4net

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Serilog、Microsoft.Extensions.Logging 和 Autofac - Serilog, Microsoft.Extensions.Logging and Autofac 无法从 Microsoft.Extensions.Logging 解析 ILogger - Unable to resolve ILogger from Microsoft.Extensions.Logging 如何获取 Microsoft.Extensions.Logging<T> 在使用 Serilog 和 AutoFac 的控制台应用程序中? - How to get Microsoft.Extensions.Logging<T> in console application using Serilog and AutoFac? 如何使用LoggerFactory和Microsoft.Extensions.Logging进行.NET Core Console使用C#进行日志记录 - How to Use LoggerFactory and Microsoft.Extensions.Logging for .NET Core Console Logging With C# 如何从 log4net 切换到 Microsoft.Extensions.Logging 多个库 - How to switch from log4net to Microsoft.Extensions.Logging over multiple libraries 如何使用 Microsoft.Extensions.Logging 配置和查看日志记录 - How to configure and view logging using Microsoft.Extensions.Logging Microsoft.Extensions.Logging - LogError 未记录异常 - Microsoft.Extensions.Logging - LogError is not logging exception 两个派生类如何共享 Microsoft.Extensions.Logging.ILogger<T> 日志框架? - How can a two derived classes share the Microsoft.Extensions.Logging.ILogger<T> logging framework? 如何从Microsoft.Extensions.Logging订阅日志 - How to subscribe to the logs from Microsoft.Extensions.Logging 如何从库代码使用 Microsoft.Extensions.Logging? - How use Microsoft.Extensions.Logging from Library code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM