简体   繁体   English

将 AddNLog 从 .NET Core 2.2 迁移到 3.0

[英]Migrate AddNLog from .NET Core 2.2 to 3.0

In the Startup class of my project I have the following Configure method:在我的项目的Startup类中,我有以下Configure方法:

private void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    ...
    loggerFactory.AddNLog();
    ...
}

This worked OK in .NET Core 2.2, but after upgrading to 3.0, I get the warning Method 'NLog.Extensions.Logging.ConfigureExtenstions.AddNLog' is obsolete: instead use ILoggingBuilder.AddNLog() or IHostBuilder.UseNLog().这在 .NET Core 2.2 中工作正常,但升级到 3.0 后,我收到警告Method 'NLog.Extensions.Logging.ConfigureExtenstions.AddNLog' is obsolete: instead use ILoggingBuilder.AddNLog() or IHostBuilder.UseNLog().

So I tried to update the method to所以我尝试将方法更新为

private void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggingBuilder loggingBuilder)
{
    ...
    loggingBuilder.AddNLog();
    ...
}

or或者

private void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostBuilder hostBuilder)
{
    ...
    hostBuilder.UseNLog();
    ...
}

But in both cases I got a DI exception: Could not resolve a service of type {Microsoft.Extensions.Logging.ILoggingBuilder/Microsoft.Extensions.Hosting.IHostBuilder} for the parameter {loggingBuilder/hostBuilder} of method 'Configure' on type 'MyProject.Startup' .但在这两种情况下,我都遇到了 DI 异常: Could not resolve a service of type {Microsoft.Extensions.Logging.ILoggingBuilder/Microsoft.Extensions.Hosting.IHostBuilder} for the parameter {loggingBuilder/hostBuilder} of method 'Configure' on type 'MyProject.Startup'

I could not find any viable source on how to change the NLog configuration fro .NET Core 3.0 and there is nothing about logging in the official Microsoft guide .我找不到任何关于如何从 .NET Core 3.0 更改 NLog 配置的可行来源,并且在Microsoft 官方指南中没有任何关于登录的内容。 Does anyone know how to solve this issue?有谁知道如何解决这个问题?

With ASP.NET Core 2+, the pattern to bootstrap an ASP.NET Core site has changed.使用 ASP.NET Core 2+,引导 ASP.NET Core 站点的模式发生了变化。 NLog adapts that. NLog 适应了这一点。 With the latest version of NLog.Extensions.Logging.ConfigureExtenstions , the old methods are made obsolete.使用最新版本的NLog.Extensions.Logging.ConfigureExtenstions ,旧方法已过时。

For example, ASP.NET Core nowadays uses a CreateHostBuilder .例如,ASP.NET Core 现在使用CreateHostBuilder

I would recommend to follow:我建议遵循:

  1. Migrate from ASP.NET Core 2.2 to 3.0 | 从 ASP.NET Core 2.2 迁移到 3.0 | Microsoft Docs 微软文档
  2. And then: Getting started with ASP.NET Core 3 · NLog/NLog Wiki然后: ASP.NET Core 3 入门 · NLog/NLog Wiki

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM