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

笑着哭i 提交于 2020-08-24 09:26:01

问题


I've updated my project from .net core 2.1 to 2.2 and then logging.AddAzureWebAppDiagnostics() in Program.cs no longer works.

    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?

Referring to 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:

So the way might be slightly different from the previous one. How do I fix this issue?


回答1:


The documentation is a bit tricky but if read carefully it become clear that following steps should be undertaken (for NET Core):

  1. Microsoft.Extensions.Logging.AzureAppServices should be installed
  2. There is NO need to call logging.AddAzureWebAppDiagnostics();
  3. Logging can be configired using following code

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

    File appsettings.json should contain

    "AzureLogging": {
         "FileName" : "azure-diagnostics-",
         "FileSizeLimit": 50024,
         "RetainedFileCountLimit": 5
    }
    
  4. Logging should be turned on on Azure Portal. After enabling, Azure Portal may ask for installing addon. Message requiring to install addon will appear on logging config page.

  1. Call logger.LogWarning ("message"); in your code to write to log file. If you use LogWarning be sure to set Level to Warning or more detailed (Info or Debug)


来源:https://stackoverflow.com/questions/54930748/logging-addazurewebappdiagnostics-does-not-work-in-net-core-2-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!