首页 > 解决方案 > 跟踪指标值应用程序洞察错误?

问题描述

我想用 nameList 和 valueList 跟踪指标,但是在运行单元测试时会显示下一个错误:

$exception {System.ArgumentException:无法处理指定的值。应为数值,但指定的 metricValue 是 Microsoft.ApplicationInsights.Metric 类型。您是否指定了正确的指标配置?

这是我的代码:

private void CreateMetric(String metricName, double Amount, List<Property> additionalProperties)
    {
        List<String> metrycsNameList = new List<String>();
        List<String> metrycsValueList = new List<String>();
        try
        {
           additionalProperties.ForEach(property => metrycsValueList.Add(property.Value));
           additionalProperties.ForEach(property => metrycsNameList.Add(property.Name));
           Metric metric = _client.GetMetric(new MetricIdentifier("MetricNamespace", metricName, metrycsNameList));

            AddMetricValues(metric, metrycsValueList);
        }
        catch (Exception ex)
        {
            throw new CustomMetricException("CustomMetricException", "Error adding a Custom Metric", ex.StackTrace);
        }

    }

// Checking dimension of the list (up to 10) and adding metrics.
    private void AddMetricValues(Metric metric, List<String> metrycsValueList)
    {
        int numberOfElements = metrycsValueList.Count;

        switch (numberOfElements)
        {
            case 1:
                metric.TrackValue(metric, metrycsValueList[0]);
                break;
            case 2:
                metric.TrackValue(metric, metrycsValueList[0], metrycsValueList[1]);
                break;
            case 3:
                metric.TrackValue(metric, metrycsValueList[0], metrycsValueList[1], metrycsValueList[2]);
                break;
            ...
     }

这就是我用列表调用方法的方式:

public void AddCustomMetricTestTupla()
{
    List<Property> propertiesList = new List<Property>();
    propertiesList.Add(new Property("propertyExample", "value"));
    propertiesList.Add(new Property("propertyExample1", "value2"));
    propertiesList.Add(new Property("propertyExample2", "value3"));

    //Tests method AddCustomMetric giving a tuple as a param.
    using (AzureInsightsClient azureInsightsClient = new AzureInsightsClient(myClientKey))
    {
        azureInsightsClient.FlushMetrics();
        azureInsightsClient.AddCustomMetric("Example", 2, propertiesList);
    }
}

有人知道我在做什么错吗?

标签: c#azureazure-application-insights

解决方案



推荐阅读