如何将自定义命名空间添加到遥测事件



根据这篇文章,我应该能够将自定义的命名空间属性附加到baseData对象。但是TelemetryClient.TrackEvent方法及其重载似乎没有提供指定命名空间属性的方法。现在,我的所有自定义事件都分类在azure.applicationinsights命名空间下。

对于度量,您应该使用GetMetricTrackValue方法来发送custom metricsTrackEvent方法实际上是针对custom events的。

要指定custom namespace property,您应该使用以下代码:

// Note, add "using Microsoft.ApplicationInsights.Metrics;" to use MetricIdentifier
MetricIdentifier id = new MetricIdentifier("Custom Metric Namespace","ComputerSold", "FormFactor", "GraphicsCard", "MemorySpeed", "BatteryCapacity", "StorageCapacity");
Metric computersSold  = _telemetryClient.GetMetric(id);
computersSold.TrackValue(110,"Laptop", "Nvidia", "DDR4", "39Wh", "1TB");

相关文档在这里。

另一种方法是使用TrackMetric方法,但现在不建议使用(原因请参阅此处(。无论如何,如果使用TrackMetric方法,代码如下所示:

MetricTelemetry metric = new MetricTelemetry();
metric.MetricNamespace = "xxxxx";
client.TrackMetric(metric);

最新更新