gpt4 book ai didi

asp.net-core - 将 ASP.NET Core ILogger<> 从内置日志机制注册到 Autofac

转载 作者:行者123 更新时间:2023-12-01 09:44:39 25 4
gpt4 key购买 nike

我有一个使用 Autofac 作为 DI 容器的 ASP.NET Core 应用程序。我还使用提供 ILogger<> 依赖项的内置日志记录机制。现在我想在 Autofac 中注册它。如何?

最佳答案

如前所述,当您使用官方 autofac-integration for asp.net-core 时你不需要做任何事情。如果您仍然想手动进行,这是合法的,您可以在 ConfigureContainer 中执行以下操作方法。

public void ConfigureContainer(ContainerBuilder builder)
{
// ... more registrations
builder.RegisterInstance(new LoggerFactory())
.As<ILoggerFactory>();

builder.RegisterGeneric(typeof(Logger<>))
.As(typeof(ILogger<>))
.SingleInstance();
}

请记住,它们没有添加特定的日志记录提供程序。

关于asp.net-core - 将 ASP.NET Core ILogger<> 从内置日志机制注册到 Autofac,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51926027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com