0
votes

I want to track metrics with nameList and valueList, but when running unit tests it shows next error:

$exception {System.ArgumentException: Cannot process the specified value. A numeric value was expected, but the specified metricValue is of type Microsoft.ApplicationInsights.Metric. Have you specified the correct metric configuration?

Here's my code:

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;
            ...
     }

And here it is how I call the method with the lists:

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);
    }
}

Anyone knows what I'm doing wrong?

1
Which line of code gives the errorChetan
Got it while debugging AddMetricValues, after case 3 (because it is dimension 3), if I add another dimension would be another case, so I think it throws it when calling TackValue.Esteban Chornet
What values you are getting in metrycsValueList[0], metrycsValueList[1], and metrycsValueList[2]. Why do you need to pass metric to it own method?Chetan
Getting "value", "value2" and "value3". I passed cause TrackValue need a metric object.Esteban Chornet
TrackValue does not need object of Metric class. docs.microsoft.com/en-us/dotnet/api/… It expects a numeric value, that's why you are getting this error.Chetan

1 Answers

0
votes

TrackValue does not need object of Metric class. docs.microsoft.com/en-us/dotnet/api/… It expects a numeric value, that's why you are getting this error.

Post by: @Chetan Ranpariya