Logging from ASP.NET 5 application hosted as Azure Web App

后端 未结 3 1773
刺人心
刺人心 2021-02-04 02:00

I have an ASP.NET 5 Web API that I host in Azure as a Web App. I want to log messages from my code using Azure Diagnostics. There are multiple article including Azure docs that

3条回答
  •  北荒
    北荒 (楼主)
    2021-02-04 02:27

    I've found an simple trick for azure app ,see https://github.com/aspnet/Logging/tree/dev/src/Microsoft.Extensions.Logging.AzureAppServices, please add the package "Microsoft.Extensions.Logging.AzureAppServices": "1.0.0-preview1-final" and update related dependencies, add the azure diagnostics in startup.cs like this :

    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();
    loggerFactory.AddAzureWebAppDiagnostics(); // for default setting.
    

    or for custom setting:

    loggerFactory.AddAzureWebAppDiagnostics(new AzureAppServicesDiagnosticsSettings( ...)); // add custom setting.
    // see here for detailed member properties: https://github.com/aspnet/Logging/blob/dev/src/Microsoft.Extensions.Logging.AzureAppServices/AzureAppServicesDiagnosticsSettings.cs
    

    And enable the diagnostics log on azure, both logging on blob and file are work well. No need for any extra configuration. :)

提交回复
热议问题