gpt4 book ai didi

azure - logger.Log 语句在 Azure 门户中的什么位置?

转载 作者:行者123 更新时间:2023-12-02 02:39:56 24 4
gpt4 key购买 nike

我有一个 .NET Core WebAPI 应用程序。该应用程序作为应用程序服务部署在 Azure 上。

在代码中,我已经启用了 Application Insights,如下所示

public static IWebHost BuildWebHost(string[] args) =>
WebHost
.CreateDefaultBuilder(args)
.UseApplicationInsights()
.UseStartup<Startup>()
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging")).SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Error);
logging.AddApplicationInsights(" xxxxx-xxxx-xxxx-xxxx--xxxxxxxxxxx").SetMinimumLevel(LogLevel.Trace);
})
.Build();

在 Controller 的构造函数和 Controller 的方法内部,我有这些日志记录语句。

_logger.LogInformation("ApiController, information");
_logger.LogWarning("ApiController, warning");
_logger.LogCritical("ApiController, critical");
_logger.LogWarning("ApiController, error");
_logger.LogDebug("ApiController, debug");

在 Azure 门户中,我为我的应用服务启用了 Application Insights。这是来自门户的图片。

App Insights in Azure Portal

但是我在 Azure 门户中哪里可以看到日志记录语句?

当我转到 Application Insights -> Logs 并通过以下方式查询时

search *

我可以看到对 API 发出的请求,但看不到日志记录语句。

Application Insights Log

日志语句在哪里?

最佳答案

首先,在代码中配置日志级别并不是一个好的做法。您可以在 appsettings.json 文件中轻松配置日志级别。因此,在 Program.cs -> public static IWebHost BuildWebHost 方法中,将代码更改为以下内容:

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseApplicationInsights()
.UseStartup<Startup>()
.Build();

然后在appsettings.json中(也右键单击文件 -> 属性 -> 将“复制到输出目录”设置为“如果较新则复制”):

{
"Logging": {
"IncludeScopes": false,
"ApplicationInsights": {
"LogLevel": {
"Default": "Trace"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
},
"ApplicationInsights": {
"InstrumentationKey": "the key"
}
}

在 Controller 中,如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()
{
_logger.LogInformation("ApiController, information");
_logger.LogWarning("ApiController, warning");
_logger.LogCritical("ApiController, critical");
_logger.LogWarning("ApiController, error");
_logger.LogDebug("ApiController, debug");

return new string[] { "value1", "value2" };
}
}

运行项目,然后等待几分钟(应用程序见解始终需要 3 到 5 分钟或更长时间才能显示数据)。然后导航到azure门户->应用程序洞察日志,记住ILogger写入的所有日志都存储在“traces”表中。只需编写“traces”之类的查询并指定适当的时间范围,您应该会看到如下所示的所有日志:

enter image description here

关于azure - logger.Log 语句在 Azure 门户中的什么位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60285667/

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