ILogger <T> .Azure函数的LogMetric未将消息记录到Application Insight中

问题描述

我正在.net core 3.x上使用Azure函数v3,其中ILogger<T>是通过通过IServiceProvider like ILogger<T> _log = _log = serviceProvider.GetService<ILogger<T>>();通过相应类T的依赖项注入而创建的

_log.LogMetric()以外,其他所有日志消息(跟踪,信息,错误,异常等)都被推送到Application Insight。我已经检查了事务搜索的跟踪部分以及日志的跟踪和自定义指标。

甚至host.json文件记录配置似乎也是正确的。那我做错了吗?

解决方法

在 Github 上有一篇关于这个的帖子表明这是一个错误。有一种解决方法可以注入 TelemetryClient 并使用 client.TrackMetric();

你可以这样做: services.AddSingleton<TelemetryClient>(x => new TelemetryClient(configuration.GetSection("ApplicationInsights:InstrumentationKey").Get<string>()))

此处的配置,即您的 IConfigurationIConfigurationRoot

然后在注入您的服务后,只需调用该方法,client.TrackMetric(metricName,metricValue,dictionaryProperties);

有问题的帖子:https://github.com/Azure/azure-webjobs-sdk/issues/2308

请注意,如果您正在使用 TelemetryClient,您将需要使用此解决方法自行汇总这些指标:

https://docs.microsoft.com/en-us/dotnet/api/microsoft.applicationinsights.telemetryclient.trackmetric?view=azure-dotnet

,

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-2.1#log-filtering

从文档中,您将发现只有信息,警告,调试,错误和跟踪将被推送到Application Insight。

这五个日志级别不包含LogMetric。