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

How to add custom Namespace to Telemetry events

根据 this post, I should be able to attach a custom namespace property to the baseData object. But the 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");

相关文档为here.

另一种方法是使用TrackMetric方法,但现在不推荐使用(原因见here)。无论如何,如果使用 TrackMetric 方法,代码如下所示:

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