【问题标题】:Cannot see Trace logs when loglevel set to Trace in azure function在 Azure 函数中将 loglevel 设置为 Trace 时看不到 Trace 日志
【发布时间】:2020-09-01 18:09:46
【问题描述】:

重现步骤:

在VS中新建一个V2函数应用 选择HTTP触发器,粘贴以下代码:

log.LogTrace("This is a trace log");
log.LogDebug("This is a debug log");
log.LogInformation("This is an information log");
log.LogWarning("This is a warning log");
log.LogError("This is an error log");
log.LogCritical("This is a critical log");
return new OkResult();

进入host.json,配置如下:

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingExcludedTypes": "Request",
      "samplingSettings": {
        "isEnabled": false
      },
      "fileLoggingMode": "always",
      "logLevel": {
        "default": "Trace",
        "Host.Results": "Error",
        "Function": "Trace",
        "Host.Aggregator": "Trace"
      }
    }
  }
}

运行主机,触发HTTP功能 预期结果 - 跟踪消息应出现在应用程序洞察中

实际结果: 在应用洞察中查询

union traces
| union exceptions
|union requests
| where timestamp > ago(30d)
| where operation_Id == 'be439155fd015344badd3839da2652a8'
| where customDimensions['InvocationId'] == '6057a32f-0f59-4193-8845-a5f9d972169f'
| order by timestamp asc
| project timestamp, message = iff(message != '', message, iff(innermostMessage != '', innermostMessage, customDimensions.['prop__{OriginalFormat}'])), logLevel = customDimensions.['LogLevel']

timestamp [UTC]             message  
8/31/2020, 4:00:58.764 PM   Executing 'Function1' (Reason='This function was programmatically called via the host APIs.', Id=6057a32f-0f59-4193-8845-a5f9d972169f)              Information 
8/31/2020, 4:00:58.764 PM   Information Logged  
8/31/2020, 4:00:58.953 PM   error Logged    
8/31/2020, 4:00:58.954 PM   Warning Logged  
8/31/2020, 4:00:58.954 PM   Executed 'Function1' (Succeeded, Id=6057a32f-0f59-4193-8845-a5f9d972169f, Duration=190ms)   

【问题讨论】:

    标签: azure asp.net-core azure-functions azure-application-insights serverless-framework


    【解决方案1】:

    那个 json 文件不正确。 fileLoggingMode 不是 applicationInsights 对象的属性。此外,不应在应用程序洞察部分中设置 Host.Result 和 Host.Aggregator 类别的日志级别。请参阅reference

    但是,除此之外,您还会遇到问题,因为函数的最低日志类别过滤应该低于或等于应用程序洞察力的日志级别。 azure 函数中的默认日志级别是 Information,因此不会通过跟踪。

    试试这个:

    {
      "version": "2.0",
      "logging": {
        "fileLoggingMode": "always",
        "logLevel": {
          "default": "Information",
          "Host.Results": "Information",
          "Function": "Trace",
          "Host.Aggregator": "Information"
        },
        "applicationInsights": {
          "samplingExcludedTypes": "Request",
          "samplingSettings": {
            "isEnabled": false
          },
          "logLevel": {
            "default": "Trace"
          }
        }
      }
    }
    

    鉴于上面的示例,函数类别的日志级别与应用程序洞察的日志级别相匹配。否则,Application Insights 日志提供程序将无法获取日志并将它们作为 TraceTelemetry 发送到 Application Insights。

    【讨论】:

    • 谢谢彼得。您的回复帮助我解决了这个问题。是的,我在 json 文件中犯了几个错误,根据您的参考,我修复了这些错误并且工作正常。再次感谢。
    • @RamanMiddha 很高兴为您提供帮助。如果答案适合您,请按照here中所述接受它
    猜你喜欢
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    • 2019-03-17
    • 2023-03-25
    相关资源
    最近更新 更多