logging.AddAzureWebAppDiagnostics() 在 .net core 2.2 中不起作用

logging.AddAzureWebAppDiagnostics() does not work in .net core 2.2

我已经将我的项目从 .net core 2.1 更新到 2.2,然后 Program.cs 中的 logging.AddAzureWebAppDiagnostics() 不再有效。

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureLogging((hostingContext, logging) =>
            {
                logging.AddAzureWebAppDiagnostics();
            })

            .UseStartup<Startup>()
            .Build();
}

'ILoggingBuilder' does not contain a definition for 'AddAzureWebAppDiagnostics' and no accessible extension method 'AddAzureWebAppDiagnostics' accepting a first argument of type 'ILoggingBuilder' could be found (are you missing a using directive or an assembly reference?

参照this document,

If targeting .NET Framework or referencing the Microsoft.AspNetCore.App metapackage, add the provider package to the project. Invoke AddAzureWebAppDiagnostics on an ILoggerFactory instance:

所以方法可能与上一个略有不同。我该如何解决这个问题?

documentation 有点棘手,但如果仔细阅读,就会清楚应该执行以下步骤(对于 NET Core):

  1. Microsoft.Extensions.Logging.AzureAppServices 应该安装

  2. 不需要调用logging.AddAzureWebAppDiagnostics();

  3. 可以使用以下代码配置日志记录

    // file startup.cs
    using Microsoft.Extensions.Logging.AzureAppServices;
    
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            //...
            services.Configure<AzureFileLoggerOptions>(Configuration.GetSection("AzureLogging"));
        } 
    }
    

    文件 appsettings.json 应包含

    "AzureLogging": {
         "FileName" : "azure-diagnostics-",
         "FileSizeLimit": 50024,
         "RetainedFileCountLimit": 5
    }
    
  4. 应在 Azure 门户上打开日志记录。启用后,Azure Portal 可能会要求安装插件。要求安装插件的消息将出现在日志配置页面上。

  1. 在您的代码中调用 logger.LogWarning ("message"); 以写入日志文件。如果您使用 LogWarning 请务必将级别设置为警告或更详细(信息或调试)