【问题标题】:Can't see logs in Azure Log Stream在 Azure 日志流中看不到日志
【发布时间】:2020-05-24 19:14:45
【问题描述】:

在我的 Web Api 应用程序中,我有控制器:

[Route("api/[controller]")]
public class ValuesController : Controller
{
    // GET api/values
    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // GET api/values/5
    [HttpGet("{id}")]
    public string Get(int id)
    {
        System.Diagnostics.Trace.TraceInformation("in get...");
        return "value";
    }
}

我希望在发布日志时能够在 Azure 上看到“in get...”日志。在 Azure 上,在App Service Logs 中打开Application Logging (Filesystem) 并将级别设置为Information。在Log Stream,当我转到方法的url时,我在日志中看到:

正在连接... 2020-02-09T06:07:38 欢迎您,您现在已连接到 日志流服务。默认超时为 2 小时。更改 应用程序设置 SCM_LOGSTREAM_TIMEOUT 超时(以秒为单位)。 2020-02-09 06:08:07.158 +00:00 [信息] Microsoft.AspNetCore.Hosting.Internal.WebHost:请求启动 HTTP/1.1 获取 http://testapi20200208104448.azurewebsites.net/api/values/5 2020-02-09 06:08:07.159 +00:00 [信息] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:正在执行 操作方法 TestApi.Controllers.ValuesController.Get (TestApi) 与 参数 (5) - ModelState 有效 2020-02-09 06:08:07.160 +00:00 [信息] Microsoft.AspNetCore.Mvc.Internal.ObjectResultExecutor: 执行ObjectResult,写入值 Microsoft.AspNetCore.Mvc.ControllerContext。

但我没有看到我的登录“登录...”

【问题讨论】:

  • .net 或 .netcore 应用程序?托管在 linux 或 windows 中?
  • .netcore 托管在 Windows 中

标签: azure


【解决方案1】:

这是.NET core 的known issue(但对于.NET framework,它可以很好地工作)。

作为 .NET Core Web 应用程序的一种解决方法,我建议您可以使用ILogger,它可以将消息写入应用程序日志。 在Startup.cs -> 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
       }

然后在ValuesController:

public class ValuesController : Controller
{

    private readonly ILogger _logger; 

   public ValuesController(ILoggerFactory loggerFactory)
   {
    _logger = loggerFactory.CreateLogger<ValuesController>();
   }
    // GET api/values
    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // GET api/values/5
    [HttpGet("{id}")]
    public string Get(int id)
    {
        //System.Diagnostics.Trace.TraceInformation("in get...");

        //use ILogger here.
        _logger.LogInformation("in get...");
        return "value";
    }
}

【讨论】:

    【解决方案2】:

    Ivan 的回答在 aspnetcore 3.1 上并不完全适合我,但它让我找到了这篇文章,它提供了我需要的所有信息:

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-3.1

    【讨论】:

      猜你喜欢
      • 2020-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多