如何在Azure中为Net Core 2 App启用应用程序日志?

问题描述:

我正在尝试以天蓝色启用应用程序日志. 我有一个虚拟的Net Core 2 App在天蓝色的appService中运行.

I am trying to enable application logs in azure. I have a dummy Net Core 2 App running in an appService in azure.

基本上,我的目标是在日志流和应用程序日志文件中查看跟踪消息,但是我没有找到正确的方法.

and basically my goal is to see the trace messages in the log stream and in the application log files but I have not found the right way to do this.

我在阅读其他文章时发现的挑战之一是,他们假设使用了适当的Web配置.

One of the challenge I have found reading other posts is that they assume a web config in place.

ASP.NET Core 2.2的文档为

The documentation for ASP.NET Core 2.2 is here.

首先,启用应用程序日志记录"并选择适当的级别:

Firstly, enable Application Logging and choose the appropriate level:

这可能是诊断任何问题所需要做的.但是,如果要查看日志消息,请安装Microsoft.Extensions.Logging.AzureAppServices NuGet程序包.

This may be all you need to do to diagnose any problems. But if you want log messages and see them, install the Microsoft.Extensions.Logging.AzureAppServices NuGet package.

然后,配置日志记录:

using Microsoft.Extensions.Logging;

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
    .ConfigureLogging(logging =>
    {
        logging.AddAzureWebAppDiagnostics();
    })
    .UseStartup<Startup>();

现在您可以注入并使用ILogger:

Now you can inject and use ILogger:

public Startup(IConfiguration configuration, ILogger<Startup> logger)
{
    Configuration = configuration;
    this.logger = logger;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    logger.LogWarning("Starting up");

然后在Azure App Service中,单击日志流":

Then in the Azure App Service, click on Log stream: