我想使用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);
}
}
有人知道我在做什么错吗?
答案 0 :(得分:0)
TrackValue
不需要Metric
类的对象。 docs.microsoft.com/en-us/dotnet/api/…它需要一个数字值,这就是为什么会出现此错误的原因。
发布者:@Chetan Ranpariya