如何将自定义消息记录到Azure门户分析监视日志

时间:2020-09-15 20:03:48

标签: c# asp.net azure .net-core azure-devops

我正在尝试在我的C#ASP点网核心Web API中添加自定义日志。我可以在Azure门户->应用程序见解->日志中找到api调用日志。

但是我无法使用以下代码找到要输入的自定义日志。在哪里寻找它们。

public async Task Invoke(HttpContext httpContext)
{
    // First, get the incoming request
    var request = await FormatRequest(httpContext.Request);

    // TODO: Save log to chosen datastore
    _logger.LogInformation('custommessage101');

    // ------
}

在日志分析查询编辑器中,我使用了以下查询,但未获取任何内容。我正在寻找的位置是否正确(Azure门户->应用程序见解->日志)?

requests | search "custommessage101"

2 个答案:

答案 0 :(得分:1)

可能是在配置记录器时,您将日志级别设置为高于信息级别。

以下设置日志级别,以便存储信息日志:

builder.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>("", LogLevel.Information);

答案 1 :(得分:0)

登录到ILogger界面的消息最终成为应用程序见解中的跟踪。查询示例为:

traces | where message == "custommessage101"

另一种选择是Search

enter image description here

发送给应用程序见解的消息的默认日志级别设置为Warning。正如设拉子(Shiraz)指出的那样,您需要将其设置为参考。您可以使用Shiraz所示的代码或通过调整appsettings.json文件来做到这一点:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information"
    },
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Information"
      }
    }
  }
}