Azure Function Application Insights 日志记录不支持 Host.json 配置

问题描述 投票:0回答:2

我已为函数应用程序启用应用程序洞察,并且已成功记录跟踪,但是当前来自所有日志级别的消息都包含在应用程序洞察跟踪表中。我已经像这样配置了我的 host.json,根据 docs 应记录“信息”及以上的 Function、Trace 及以上的 Host.Results,没有其他内容。

{
 "version": "2.0",
 "logging": {
  "logLevel": {
    "default": "None",
    "Host.Results": "None",
    "Function": "Information",
    "Host.Aggregator": "Trace"
  },
  "applicationInsights": {
    "samplingSettings": {
      "isEnabled": true,
      "excludedTypes": "Dependency;Request;PageView",
      "maxTelemetryItemsPerSecond" : 20,
      "httpAutoCollectionOptions": {
        "enableHttpTriggerExtendedInfoCollection": true,
        "enableW3CDistributedTracing": false,
        "enableResponseHeaderInjection": false
      }
    }
  }
},
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[3.*, 4.0.0)"
  },
  "functionTimeout": "02:00:00",
   "extensions": {
   "queues": {
            "maxPollingInterval": "00:00:01",
            "visibilityTimeout" : "00:00:15",
            "batchSize": 1,
            "newBatchThreshold": 2,
            "maxDequeueCount": 2
        }
    }
}

我还使用以下 logLevel 值进行了测试,这应该可以防止记录任何痕迹

"logLevel": {
    "default": "None",
  },

最后,我尝试在应用程序洞察部分以及日志记录部分中定义 logLevel,如推荐的 here 所示,但这也没有效果,并且似乎不是 host.json 的有效配置参考.

修改 logLevel 部分确实在本地运行函数时正确更改控制台日志级别,但对 Application Insights 中记录的内容没有影响。我将非常感谢任何有关此问题的帮助。我们目前无法使用应用程序洞察,因为记录的数据量太大,导致我们的成本上升。

编辑:添加了完整的host.json

azure-functions azure-application-insights host.json
2个回答
0
投票

我创建了一个功能应用程序并将应用程序见解集成到其中。

  • 然后我在本地创建了一个 Timmer 触发功能应用程序,并通过添加检测密钥连接到应用程序洞察。

  • 我能够运行函数应用程序并在我的见解中跟踪日志,但是,我只能在同一个跟踪表中获取所有默认和函数日志。因此,为了克服这个问题,我已经采取了日志级别,请检查以下文件。

主机.json:

{
    "version": "2.0",
    "logging": {
      "logLevel": {
        "default": "Information",
        "Host.Results": "Information",
        "Function": "Information",
        "Host.Aggregator": "Trace",
        "Function.Function1.User": "Trace",
        "Function.Function1.System": "Debug"

      },
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Dependency;Request;PageView",
        "maxTelemetryItemsPerSecond": 20,
        "httpAutoCollectionOptions": {
          "enableHttpTriggerExtendedInfoCollection": true,
          "enableW3CDistributedTracing": false,
          "enableResponseHeaderInjection": false
        }
      }
    }
  },
  "functionTimeout": "02:00:00",
  "extensions": {
    "queues": {
      "maxPollingInterval": "00:00:01",
      "visibilityTimeout": "00:00:15",
      "batchSize": 1,
      "newBatchThreshold": 2,
      "maxDequeueCount": 2
    }
  }
}
  • 下面是我的示例 function app 代码。
using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

public static class Function1
{
    [FunctionName("Function1")]
    public static void Run([TimerTrigger("0 */1 * * * *")] TimerInfo timer, ILogger logger)
    {

        logger.LogInformation("This is an information-level log message.");
        logger.LogWarning("This is a warning-level log message.");
        logger.LogError("This is an error-level log message.");
        logger.LogTrace("This is a trace-level log message.");
        logger.LogDebug("This is a debug-level log message.");
        logger.LogCritical("This is a critical-level log message.");
    }
}
  • 我能够通过与host.json文件通信成功运行。

enter image description here enter image description here

  • 我能够查看按安全级别分隔的功能日志

查询:

traces
| where severityLevel in (0, 1, 2, 3, 4, 5)
| order by timestamp desc

结果: enter image description here enter image description here


0
投票

根据隔离的Azure功能中的ms doc,您需要删除默认的应用程序洞察过滤器。然后您就可以通过

host.json

配置日志记录级别
var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .ConfigureServices(s =>
    {
        s.AddApplicationInsightsTelemetryWorkerService();
        s.ConfigureFunctionsApplicationInsights();
        s.Configure<LoggerFilterOptions>(options =>
        {
            // The Application Insights SDK adds a default logging filter that instructs ILogger to capture only Warning and more severe logs. Application Insights requires an explicit override.
            // Log levels can also be configured using appsettings.json. For more information, see https://learn.microsoft.com/en-us/azure/azure-monitor/app/worker-service#ilogger-logs
            LoggerFilterRule toRemove = options.Rules.FirstOrDefault(rule => rule.ProviderName
                == "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");

            if (toRemove is not null)
            {
                options.Rules.Remove(toRemove);
            }
        });
    })
    .Build();
© www.soinside.com 2019 - 2024. All rights reserved.