using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Extensions.Configuration;
using System;
...
public class AdditionalPropertyTelemetryInitializer : ITelemetryInitializer
{
private readonly IConfiguration _configuration;
public AdditionalPropertyTelemetryInitializer(IConfiguration configuration)
{
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
}
public void Initialize(ITelemetry telemetry)
{
var requestTelemetry = telemetry as RequestTelemetry;
// Here we filter on request telemetry
if (requestTelemetry == null) return;
requestTelemetry.Properties.Add("BuildNumber", _configuration["BuildNumber"]);
}
}
型 最后一件事是注册TelemetryInitializer:
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
[assembly: FunctionsStartup(typeof(FunctionApp1.Startup))]
namespace FunctionApp1
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddSingleton<ITelemetryInitializer, AdditionalPropertyTelemetryInitializer>();
}
}
}
1条答案
按热度按时间wnavrhmk1#
您可以创建一个自定义的
ITelemetryInitializer
来添加其他属性。这里我创建了一个名为
BuildNumber
的应用程序设置:字符串
和TelemetryInitializer,然后从配置中获取其他属性。
型
最后一件事是注册TelemetryInitializer:
型
我现在可以在应用程序洞察遥测上看到BuildNumber: