gpt4 book ai didi

azure - 无法查看 Azure 日志流中的日志

转载 作者:行者123 更新时间:2023-12-02 19:41:02 27 4
gpt4 key购买 nike

在我的 Web Api 应用程序中,我有 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 上,在应用服务日志中,我打开应用程序日志记录(文件系统)并将级别设置为信息。在日志流中,当我转到该方法的 url 时,我在日志中看到:

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.

但我没有看到我的日志“在获取...”

最佳答案

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

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

然后在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 - 无法查看 Azure 日志流中的日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60133976/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com