如何在Azure中为Net Core 2 App启用应用程序日志? [英] How to enable Application Logs in Azure for Net Core 2 App?

查看:87
本文介绍了如何在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:

这篇关于如何在Azure中为Net Core 2 App启用应用程序日志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆