我正在 .NET Core 3.1 中开发 Azure 函数。我注意到它不会将所有所需的数据保存到 Azure Insights 中。可以肯定的是,我尝试在本地主机上运行我的函数,不仅向 AI 添加日志记录,而且每次我尝试将数据记录到 AI 时,我还创建了一个包含我想要的数据的文本文件登录到.创建的文本文件的数量与我的预期完全相同,而我在 AI 日志中的条目却少了很多。
为了禁用采样,我尝试了host.json文件的各种配置,该文件的当前内容是:
"version": "2.0",
"functionTimeout": "-1",
"logging": {
"fileLoggingMode": "always",
"logLevel": {
"default": "Information"
},
"applicationInsights": {
"samplingSettings": {
"isEnabled": false,
"maxTelemetryItemsPerSecond": 10000,
"excludedTypes": "Debug,Trace,Warning,Error,Information,Critical"
}
}
}
}
(由于将 isEnabled 设置为 false 没有帮助,我也尝试设置每秒足够数量的最大遥测项目,但它也没有帮助)
它不起作用(无论是在我的本地计算机上还是部署在Azure上);我也尝试通过代码禁用它:
namespace MyAzureFunction
{
public class MyStartup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
var configDescriptor = builder.Services.SingleOrDefault(tc => tc.ServiceType == typeof(TelemetryConfiguration));
if (configDescriptor?.ImplementationFactory != null)
{
var aiOptions = new ApplicationInsightsServiceOptions();
aiOptions.EnableAdaptiveSampling = false;
var implFactory = configDescriptor.ImplementationFactory;
builder.Services.Remove(configDescriptor);
builder.Services.AddSingleton(provider =>
{
if (implFactory.Invoke(provider) is TelemetryConfiguration config)
{
var newConfig = TelemetryConfiguration.Active;
newConfig.ApplicationIdProvider = config.ApplicationIdProvider;
newConfig.InstrumentationKey = config.InstrumentationKey;
return newConfig;
}
return null;
});
builder.Services.AddApplicationInsightsTelemetry(aiOptions);
}
}
}
另外,当我在本地主机上运行我的函数时,这就是我在命令行窗口中看到的:
[2022-08-24T13:37:55.249Z] {
[2022-08-24T13:37:55.250Z] "version": "2.0",
[2022-08-24T13:37:55.252Z] "functionTimeout": "-1",
[2022-08-24T13:37:55.253Z] "logging": {
[2022-08-24T13:37:55.254Z] "fileLoggingMode": "always",
[2022-08-24T13:37:55.255Z] "logLevel": {
[2022-08-24T13:37:55.257Z] "default": "Information"
[2022-08-24T13:37:55.258Z] },
[2022-08-24T13:37:55.259Z] "applicationInsights": {
[2022-08-24T13:37:55.260Z] "samplingSettings": {
[2022-08-24T13:37:55.262Z] "isEnabled": false,
(...)
[2022-08-24T13:37:57.043Z] Initializing Host. OperationId: ...
[2022-08-24T13:37:57.055Z] Host initialization: ConsecutiveErrors=0, StartupCount=1, OperationId=...
[2022-08-24T13:37:57.087Z] ApplicationInsightsLoggerOptions
[2022-08-24T13:37:57.089Z] {
[2022-08-24T13:37:57.089Z] "SamplingSettings": null,
[2022-08-24T13:37:57.090Z] "SamplingExcludedTypes": null,
[2022-08-24T13:37:57.091Z] "SamplingIncludedTypes": null,
[2022-08-24T13:37:57.092Z] "SnapshotConfiguration": null,
如您所见,即使它在关闭采样的情况下读取主机配置,SamplingSettings 仍为空,但我找不到有效禁用它的方法。
我已尝试重现您的问题。禁用采样后,我能够在应用程序见解中获取所有遥测信息。
我使用了与您相同的
host.json
文件配置。
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": false,
"maxTelemetryItemsPerSecond": 10000,
"excludedTypes": "Debug,Trace,Warning,Error,Information,Critical"
}
}
}
}
startup.cs
var aiOptions = new ApplicationInsightsServiceOptions();
aiOptions.EnableAdaptiveSampling = false;
builder.Services.AddApplicationInsightsTelemetry(aiOptions);
我从本地和Azure得到的结果: 当地的
在Azure中部署相同的功能
host.json
KUDU 中生成的日志文件
人工智能结果
排除类型属性的采样设置似乎未正确设置。
可识别的类型有:Dependency、Event、Exception、PageView、Request、Trace。