在Azure函数中将日志级别设置为“跟踪”时,看不到跟踪日志

问题描述

复制步骤:

在VS中创建一个新的V2功能应用程序 选择HTTP触发器,然后粘贴以下代码

log.LogTrace("This is a trace log");
log.LogDebug("This is a debug log");
log.Log@R_122_4045@ion("This is an @R_122_4045@ion 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']

logs

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)              @R_122_4045@ion 
8/31/2020,4:00:58.764 PM   @R_122_4045@ion 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)   

解决方法

该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"
      }
    }
  }
}

在“功能”类别的日志级别上方给出的示例与应用程序见解的日志级别匹配。否则,应用程序见解日志提供程序将无法提取日志并将其作为TraceTelemetry发送到应用程序见解。