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

sab*_*iel 6 c# logging azure asp.net-core-2.0

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

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

我发现阅读其他文章时遇到的挑战之一是,他们假设就位了Web配置。

家庭控制器 在Azure中配置 启动文件

Mat*_*ear 10

ASP.NET Core 2.2 的文档在这里

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

开启应用程序日志

这可能是您诊断任何问题所需要做的全部工作。但是,如果您想要日志消息并查看它们,请安装 Microsoft.Extensions.Logging.AzureAppServices NuGet 包。

然后,配置日志记录:

using Microsoft.Extensions.Logging;

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

现在您可以注入和使用 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");
Run Code Online (Sandbox Code Playgroud)

然后在 Azure 应用服务中,单击日志流: 日志流


Tom*_*SFT 1

你可以从这个博客得到答案。以下是博客的片段。

\n\n
\n

在 ASP.NET Core 应用程序中设置日志记录不需要太多代码。ASP.NET Core 新项目模板已经在 Startup.Configure 方法中使用以下代码设置了一些基本的日志记录提供程序:

\n
\n\n
loggerFactory.AddConsole(Configuration.GetSection("Logging")); \nloggerFactory.AddDebug();\n
Run Code Online (Sandbox Code Playgroud)\n\n

在此输入图像描述

\n