【问题标题】:how to log custom messages to azure portal analytics monitoring logs如何将自定义消息记录到 Azure 门户分析监控日志
【发布时间】:2021-01-02 15:20:54
【问题描述】:

我正在尝试在我的 c# asp dot net core 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"

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      记录到ILogger 接口的消息最终会作为应用程序洞察中的跟踪。一个示例查询是:

      traces | where message == "custommessage101"
      

      另一个选项是Search:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-03
        • 2023-04-02
        • 1970-01-01
        相关资源
        最近更新 更多