如何在Azure上查看应用程序日志记录

时间:2018-09-30 13:24:40

标签: c# azure asp.net-core asp.net-core-2.1

我有一个在Azure上运行的aspnetcore 2.1应用程序。

我现在想查看日志记录信息以调试仅在Azure上发生的问题。

在应用程序中,将ILogger <>注入到类中并使用:    this._logger.LogInformation("constructor**********************************************");

如果我在VS中运行该应用程序,则可以在调试输出窗口和asp.net核心Web服务器输出窗口中看到输出。

然后我发布并继续使用Azure并启用日志流并对其进行查看。我确实看到信息出现在日志流中,但这仅仅是来自IIS的请求信息。我没有看到其他日志消息。

要查看Azure上的日志记录信息,还需要做其他事情吗?

3 个答案:

答案 0 :(得分:3)

您可以使用Microsoft.Extensions.Logging.AzureAppServices。该软件包将自己描述为:

  

记录程序实现,以支持Azure App Services的“诊断日志”和“日志流”功能。

安装该软件包后,您需要更新记录器配置以使用此新提供程序,通常在Program.cs中完成,如下所示:

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

答案 1 :(得分:1)

您需要为应用程序配置Application Insights,以查看生成的日志。

您可以阅读Enable diagnostics logging for web apps in Azure App Service

中的步骤

答案 2 :(得分:0)

您需要为项目安装和配置Azure Application Insights。

通过在应用程序中配置仪器密钥,您可以将各种日志从应用程序发送到Application Insights。

安装下面的Nuget

Microsoft.ApplicationInsights.AspNetCore 2.2.0 NuGet packages

在asp.net核心应用程序中配置Application Insights

public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
// Change to file post adding Application Insights Telemetry:
                .UseApplicationInsights()
//
                .UseStartup<Startup>()
                .Build();
    }

See this step by step instruction on how to configure application insights into your asp.net core