如何在应用程序洞察中将自定义属性添加到默认请求遥测?我能够通过创建新的遥测客户端来做到这一点,但我不想这样做,因为它会创建重复的事件。
答案 0 :(得分:1)
制作您自己的自定义遥测初始化器。 https://azure.microsoft.com/en-us/documentation/articles/app-insights-api-custom-events-metrics/#telemetry-initializers
从上面的文章中摘过:
using System;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
namespace MvcWebRole.Telemetry
{
/*
* Custom TelemetryInitializer that overrides the default SDK
* behavior of treating response codes >= 400 as failed requests
*
*/
public class MyTelemetryInitializer : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
var requestTelemetry = telemetry as RequestTelemetry;
// Is this a TrackRequest() ?
if (requestTelemetry == null) return;
int code;
bool parsed = Int32.TryParse(requestTelemetry.ResponseCode, out code);
if (!parsed) return;
if (code >= 400 && code < 500)
{
// If we set the Success property, the SDK won't change it:
requestTelemetry.Success = true;
// Allow us to filter these requests in the portal:
requestTelemetry.Context.Properties["Overridden400s"] = "true";
}
// else leave the SDK to set the Success property
}
}
}
然后在配置文件中或通过代码加载初始化程序,请参阅上面的文档了解这些细节。