在Azure日志流中看不到日志 [英] Can't see logs in Azure Log Stream

查看:90
本文介绍了在Azure日志流中看不到日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my Web Api App, I have the controller:

[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上,在应用程序服务日志中,打开应用程序日志记录(文件系统),并将级别设置为 Information .在 Log Stream 中,当我转到方法的url时,我在日志中看到:

I want to be able to see the log "in get..." on Azure when I publish it. On Azure, in the App Service Logs I turn on Application Logging (Filesystem) and set the level to Information. In the Log Stream, when I go to the url of the method, I see in the logs:

正在连接... 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 GET http://testapi20200208104448.azurewebsites.net/api/values/5 2020-02-0906: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.

Connecting... 2020-02-09T06:07:38 Welcome, you are now connected to log-streaming service. The default timeout is 2 hours. Change the timeout with the App Setting SCM_LOGSTREAM_TIMEOUT (in seconds). 2020-02-09 06:08:07.158 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://testapi20200208104448.azurewebsites.net/api/values/5 2020-02-09 06:08:07.159 +00:00 [Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Executing action method TestApi.Controllers.ValuesController.Get (TestApi) with arguments (5) - ModelState is Valid 2020-02-09 06:08:07.160 +00:00 [Information] Microsoft.AspNetCore.Mvc.Internal.ObjectResultExecutor: Executing ObjectResult, writing value Microsoft.AspNetCore.Mvc.ControllerContext.

但是我看不到我的日志"..."

But I don't see my log "in get..."

推荐答案

这是.NET核心的已知问题(但对于.NET框架,它可以正常工作).

This is a known issue for .NET core(but for .NET framework, it can work well).

作为.NET核心Web应用程序的一种解决方法,我建议您可以使用 ILogger ,它可以将消息写入应用程序日志.在 Startup.cs -> Configure 方法中,重新编写Configure方法,如下所示:

As a workaround for .NET core web application, I suggest you can use ILogger, which can write message to Application Logs. In Startup.cs -> Configure method, re-write Configure method like below:

        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";
    }
}

这篇关于在Azure日志流中看不到日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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