【问题标题】:What to See Trace Logs for Azure App Service?要查看 Azure 应用服务的跟踪日志的内容?
【发布时间】:2020-04-27 12:17:54
【问题描述】:

Azure App Service 中使用.NET Core 2。看不到我的日志。在实际应用中,日志代码如下所示:

using System.Diagnostics;
...
Trace.WriteLine("Logging works");

我希望在Log Stream 中看到Trace 日志,但我没有。我确实看到了一般的 API 日志。我究竟做错了什么?我的配置如下:

【问题讨论】:

标签: azure logging .net-core azure-app-service-plans


【解决方案1】:

我对 .NET core azure web 应用程序有同样的问题:Trace.WriteLine 方法对 .NET core 不起作用(不向应用程序日志写入消息),但对 .NET framework 起作用,我找到了 issue关于那个。

作为 .NET core web 应用的一种解决方法,我建议你可以使用 ILogger,它可以将消息写入Application Logs

Startup.cs -> Configure 方法中,重写Configure 方法如下:

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
           //your other code

            //add the following 2 lines of code.
            loggerFactory.AddConsole();
            loggerFactory.AddDebug();


            app.UseStaticFiles();

            //your other code
}

然后在 HomeController.cs 中,添加如下代码:

public class HomeController : Controller
{
    private readonly ILogger _logger; 

    public HomeController(ILoggerFactory loggerFactory)
    {
        _logger = loggerFactory.CreateLogger<HomeController>();
    }

    public IActionResult Index()
    {
        _logger.LogInformation("this is a information from ILogger...");

        return View();
    }

    //other code

  }

发布到 azure 后,配置应用程序日志 -> 运行 azure web 应用程序 -> 可以看到消息显示在应用程序日志中:

【讨论】:

  • 非常感谢伊万。这个周末我会试试的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-01
  • 2011-12-21
  • 2021-03-25
  • 1970-01-01
  • 2019-12-29
  • 2018-09-07
相关资源
最近更新 更多