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?
metrycsValueList[0]
,metrycsValueList[1]
, andmetrycsValueList[2]
. Why do you need to passmetric
to it own method? – ChetanTrackValue
does not need object ofMetric
class. docs.microsoft.com/en-us/dotnet/api/… It expects a numeric value, that's why you are getting this error. – Chetan