为什么即使在 Azure 中配置了所需的设置后我也看不到应用程序服务中的日志?

Why I do not see logs in app service even after configuring required settings in Azure?

我的 Web 应用程序中有以下两个语句,使用 NET Core

构建
System.Diagnostics.Trace.TraceError("header:" + certHeader);

_logger.LogInformation("Certificate with thumbprint");

我已将其发布到 Azure App Service,这是我的设置

当我尝试使用 kudu scm

查看时,Application 文件夹仍然是空的

我什至尝试使用 visual studio 中的 view streaming log 选项。它没有打印任何日志:(

这里是web.config

  <?xml version="1.0" encoding="utf-8"?>
  <configuration>
  <system.webServer>        
      <handlers>
          <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/>
      </handlers>
      <httpPlatform processPath="%DNX_PATH%" arguments="%DNX_ARGS%" stdoutLogEnabled="true" startupTimeLimit="3600"/>
  </system.webServer>

那么我到底在哪里可以看到我的日志?

我相信如果响应状态为 200 OK,Trace 可能不会记录?只是猜测

在 asp.net 核心网络应用程序中,我们使用 ILogger 进行日志记录。更多详情 here.

你在azure portal中的设置没问题,只需要在你的项目中添加一些代码,示例如下:

在 Startup.cs -> Configure 方法中,该方法如下所示(此处使用 ILoggerFactory):

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory logger)
    {
        if (env.IsDevelopment())
        {
            app.UseBrowserLink();
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

       //add the following two lines of code
       logger.AddConsole();
       logger.AddDebug();

        app.UseStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }

然后在 controller.cs:

      public class HomeController : Controller
        {
            private readonly ILogger _logger;
            public HomeController(ILoggerFactory loggerFactory)
            {
                _logger = loggerFactory.CreateLogger<HomeController>();
            }

            public IActionResult Index()
            {
                return View();
            }

            public IActionResult About()
            {
                _logger.LogInformation("ILogger --- this is from contact page!!!! info");
            _logger.LogError("ILogger --- this is from contact page xxxx error");                

            return View();
            }

           //other code
    }

您也可以在项目的appsettings.json文件中更改日志级别,默认级别为警告。

发布到azure后,可以在Log Stream中看到ILogger记录的日志。