如何修复过时的 ILoggerFactory 方法? [英] How to fix obsolete ILoggerFactory methods?

查看:35
本文介绍了如何修复过时的 ILoggerFactory 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将我的项目升级到 .NET Core 2.2.x 并收到关于以下代码的过时警告 - 两行:

I upgraded my project to .NET Core 2.2.x and got an obsolete warning regarding the following code - both lines:

public void Configure(IApplicationBuilder app, 
                      IHostingEnvironment env, 
                      ILoggerFactory loggerFactory) 
  {
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));

修复的建议是推荐的替代方法是 AddConsole(这个 ILoggingBuilder 构建器).我认为这就是我正在使用的.

The suggestion to fix is The recommended alternative is AddConsole(this ILoggingBuilder builder). I thought that is what I am using.

我在这里遗漏了什么?

推荐答案

我今天遇到了同样的问题.

I had the same issue today.

从 Startup.cs 中删除您的日志记录配置并转到您的 Program.cs 文件并添加如下内容:

Remove your logging configuration from Startup.cs and go to your Program.cs file and add something like:

var host = new WebHostBuilder()
    .UseKestrel()
    .UseContentRoot(Directory.GetCurrentDirectory())
    .UseIISIntegration()
    .UseStartup<Startup>()
    .ConfigureLogging((hostingContext, logging) =>
    {
        logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
        logging.AddConsole();
        logging.AddDebug();
    })
    .Build();

这里使用了 'builder',因为变量 'logging' 是一个 IloggingBuilder(而您的代码仍在使用 ILoggerFactory)

This used the 'builder' because the variable 'logging' is an IloggingBuilder (whereas your code is still using ILoggerFactory)

更新:我刚刚尝试的另一种方法是留在 Startup.cs 中,但将日志内容从Configure"方法移动到ConfigureServices",如下所示:

UPDATE: The other method I just tried is to stay inside Startup.cs but move the logging stuff from the 'Configure' method to 'ConfigureServices' like this:

public void ConfigureServices(IServiceCollection services)
{
    services.AddLogging(loggingBuilder =>
    {
        loggingBuilder.AddConfiguration(Configuration.GetSection("Logging"));
        loggingBuilder.AddConsole();
        loggingBuilder.AddDebug();
    });
}

也许可以减少 Program.cs 的污染...

Perhaps keeps the Program.cs less polluted...

这篇关于如何修复过时的 ILoggerFactory 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆