简体   繁体   English

如何在 .NET 6 Program.cs 类中获取 ILoggerFactory 实例

[英]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:其他选项:

  • switch back to the generic hosting切换回通用主机
  • rework 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.

相关问题 .NET Core 6 - 如何在启动期间在 Program.cs 中获取没有依赖注入的 ILogger 实例 - .NET Core 6 - How to get an ILogger instance without Dependency Injection in Program.cs during Startup 如何在 .NET 6 Program.cs 中创建记录器 - How to create a Logger in .NET 6 Program.cs "如何在 .NET 6 中使用 Program.cs 文件" - How to use Program.cs file in .NET 6 如何在不使用启动 class 的情况下将其迁移到我的 .NET 6 Program.cs 文件中? 代码来自 .NET Core 2.1,也在 Program.cs - How do I migrate this into my .NET 6 Program.cs file without using the startup class? The code is from .NET Core 2.1, also in Program.cs 如何在Console App .NET 6.0的Program.cs中全局声明static实例 - How to declare static instance globally in Program.cs of Console App .NET 6.0 在 .net 6 - Calling class function from program.cs at runtimes in .net 6 如何在 .net 6 中获取 ILoggerFactory - How to get ILoggerFactory in .net 6 C# - 如何从 Program.cs 获取 Windows 上 Program.cs 所在目录的路径 - C# - how to get path to directory where Program.cs is on Windows from Program.cs 如何访问用户管理器<identityuser>来自 asp.net 核心 7.0 中 program.cs 中的 WebApplication 实例应用程序的实例</identityuser> - How to access UserManager<IdentityUser> instance from WebApplication instance app in program.cs in asp.net core 7.0 如何在 .NET 6 的 Program.cs 中处理请求管道和服务注册 - How request pipeline and service registration is handled in Program.cs in .NET 6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM