在 azure 函数中将日志级别设置为 Trace 时无法看到 Trace 日志 [英] Cannot see Trace logs when loglevel set to Trace in azure function

查看:80
本文介绍了在 azure 函数中将日志级别设置为 Trace 时无法看到 Trace 日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重现步骤:

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

log.LogTrace("这是一个跟踪日志");log.LogDebug("这是一个调试日志");log.LogInformation(这是一个信息日志");log.LogWarning(这是一个警告日志");log.LogError(这是一个错误日志");log.LogCritical(这是一个关键日志");返回新的 OkResult();

进入host.json,配置如下:

{版本":2.0",记录":{应用洞察":{samplingExcludedTypes":请求",采样设置":{isEnabled":假},fileLoggingMode":总是",日志级别":{默认":跟踪",Host.Results":错误",功能":跟踪",Host.Aggregator":跟踪"}}}}

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

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

联合跟踪|工会例外|联合请求|其中时间戳 >前(30天)|其中 operation_Id == 'be439155fd015344badd3839da2652a8'|其中 customDimensions['InvocationId'] == '6057a32f-0f59-4193-8845-a5f9d972169f'|按时间戳 asc 排序|项目时间戳,message = iff(message != '', message, iff(innermostMessage != '',innermostMessage, customDimensions.['prop__{OriginalFormat}'])), logLevel = customDimensions.['LogLevel']

时间戳 [UTC] 消息8/31/2020, 4:00:58.764 PM Executing 'Function1' (Reason='这个函数是通过主机 API 以编程方式调用的.', Id=6057a32f-0f59-4193-8845-a5f9d972169f) 信息2020 年 8 月 31 日下午 4:00:58.764 记录信息8/31/2020, 4:00:58.953 PM 错误记录2020 年 8 月 31 日下午 4:00:58.954 记录警告2020 年 8 月 31 日下午 4:00:58.954 执行Function1"(成功,Id=6057a32f-0f59-4193-8845-a5f9d972169f,持续时间=190ms)

解决方案

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

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

试试这个:

<代码>{版本":2.0",记录":{fileLoggingMode":总是",日志级别":{默认":信息",Host.Results":信息",功能":跟踪",Host.Aggregator":信息"},应用洞察":{samplingExcludedTypes":请求",采样设置":{isEnabled":假},日志级别":{默认":跟踪";}}}}

鉴于上面的示例,Function 类别的日志级别与应用程序洞察的日志级别相匹配.否则,应用洞察日志提供程序将无法获取日志并将其作为 TraceTelemetry 发送到应用洞察.

Steps to reproduce:

Create a new V2 function app in VS Select HTTP trigger, paste in the following code:

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();

Go to host.json, configure as follows:

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

Run the host, trigger the HTTP function Expected result - trace message should appear in the application insights

Actual result: Query in Application insights

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)   

解决方案

That json file is not correct. fileLoggingMode is not a property of the applicationInsights object. Also, the log levels for the Host.Result and Host.Aggregator categories should not be set inside the application insights section. See the reference.

But, besides that, you are having issues because the minimum log category filtering for functions should be lower or equal to the log level of application insights. The default log level in azure functions is Information so a trace won't pass through.

Try this:

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

Given the example above the log level for the Function category matches the log level of application insights. Otherwise the application insights log provider won't be able to pick up the logs and send them as TraceTelemetry to Application Insights.

这篇关于在 azure 函数中将日志级别设置为 Trace 时无法看到 Trace 日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆