首页 > 解决方案 > 使用遥测客户端的 Get-Metric 方法出现错误“尝试通过指定 2 个维度来获取指标系列,但此指标有 3 个维度”

问题描述

  string source = string.IsNullOrEmpty(this.logger.PStore[Constants.CurrentKey]) ? "NA" : this.logger.PStore[Constants.CurrentKey];
  string cId = string.IsNullOrEmpty(this.logger.Store[Constants.CId]) ? "NA" : this.logger.Store[Constants.CId];


public virtual void GetMetric(string metricId, double metricValue, string source = "", string cId = "")
    {
        Metric metric = this.TelemetryClient.GetMetric(metricId, "Source", "CorrelationId");
        metric.TrackValue(metricValue, source, cId);
    }
      

我用于在应用程序洞察力中记录数据的上述代码。GetMetric 具有 2 个维度和一个 metricId,Track 值具有值和维度。

Microsoft.ApplicationInsights,版本=2.17.0.146

标签: azureazure-functionsazure-application-insightsazure-log-analyticsappinsights

解决方案


事实上,除了metricId,这个度量有 3 个维度metricValuesourcecId

MetricIdentifier当维度超过三个时可以使用:

MetricIdentifier id = new MetricIdentifier("metricNamespace", "metricId","metricValue", "source ", "cId");
Metric metric = client.GetMetric(cId);
metric.TrackValue(metricNamespace,metricID,metricValue, source, cId);

三个以上维度可以参考TelemetryClient.GetMetric 方法TrackMetric以及如何使用 MetricIdentifier


推荐阅读