[英]How to get ILoggerFactory instance in .NET 6 Program.cs class
In .NET 5 ILoggerFactory
can be accessed in the Startup constructor as follows:在 .NET 5 中,可以在 Startup 构造函数中访问
ILoggerFactory
,如下所示:
public Startup(
IConfiguration configuration,
IHostEnvironment hostEnvironment,
ILoggerFactory loggerFactory)
{
LoggerFactory = loggerFactory;
Configuration = configuration;
HostEnvironment = hostEnvironment;
}
I just have a custom package that receives as a parameter the ILoggerFactory
instance我只有一个自定义包,它接收
ILoggerFactory
实例作为参数
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddCustomMvc(Configuration, loggerFactory: LoggerFactory);
}
In .NET 6 how can I get the ILoggerFactory
before the app.Build()
like this在 .NET 6 中,我怎样才能像这样在
app.Build()
之前获得ILoggerFactory
var builder = WebApplication.CreateBuilder(args);
var configuration = builder.Configuration;
builder.Services.AddCustomMvc(configuration, loggerFactory: loggerFactory);
var app = builder.Build();
app.Run();
I'm afraid that there is not much options except creating ILoggerFactory
manually (as done to log in top-level statement Program
file):恐怕除了手动创建
ILoggerFactory
之外没有太多选择(就像登录顶级语句Program
文件所做的那样):
using var loggerFactory =
LoggerFactory.Create(lb => lb.AddConfiguration(builder.Configuration));
builder.Services.AddCustomMvc(configuration, loggerFactory: loggerFactory);
Which will require you to repeat the logging setup.这将要求您重复记录设置。
Other options:其他选项:
AddCustomMvc
approach so there would be no need to pass the loggerFactory
(change registrations to resolve from IServiceProvider
)AddCustomMvc
方法,因此无需传递loggerFactory
(更改注册以从IServiceProvider
解析)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.