You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PowerShell为Application Insights指标添加属性时Add方法报错问题

问题:向Azure Application Insights指标添加自定义维度时Add方法报错

我正在编写向Azure Application Insights发送指标的PowerShell脚本,基础发送代码可正常运行:

$telemetryClient = [Microsoft.ApplicationInsights.TelemetryClient]::new()
$telemetryClient.TelemetryConfiguration.ConnectionString = 'InstrumentationKey=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/'

$Sample = [Microsoft.ApplicationInsights.DataContracts.MetricTelemetry]::new()
$Sample.Name = 'TotalAppRegs'
$Sample.Timestamp = $now
$Sample.Value = 100

$telemetryClient.TrackMetric($Sample)
$telemetryClient.Flush()

尝试为指标添加自定义维度(Custom Dimensions)时,修改代码如下:

$telemetryClient = [Microsoft.ApplicationInsights.TelemetryClient]::new()
$telemetryClient.TelemetryConfiguration.ConnectionString = 'InstrumentationKey=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/'

$Sample = [Microsoft.ApplicationInsights.DataContracts.MetricTelemetry]::new()
$Sample.Name = 'TotalAppRegs'
$Sample.Timestamp = $now
$Sample.Value = 100
$Sample.Properties.Add('key','value')

$telemetryClient.TrackMetric($Sample)
$telemetryClient.Flush()

运行时报错:

MethodException: Cannot find an overload for "Add" and the argument count: "2"

查看$Sample.Properties的Add方法定义:

PS: $Sample.Properties|gm add|fl       

TypeName   : System.Collections.Concurrent.ConcurrentDictionary`2[[System.String, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral,
             PublicKeyToken=7cec85d7bea7798e]]
Name       : Add
MemberType : Method
Definition : void IDictionary[string,string].Add(string key, string value), void ICollection[KeyValuePair[string,string]].Add(System.Collections.Generic.KeyValuePair[string,string] item), void IDictionary.Add(System.Object key, System.Object value)

原因分析

$Sample.Properties是ConcurrentDictionary<string, string>类型,它的Add(string key, string value)方法是显式实现了IDictionary<string, string>接口的方法,PowerShell默认无法直接调用这种显式接口实现的方法,因此会找不到匹配的重载。

解决方法

方法1:使用索引器赋值(推荐)

ConcurrentDictionary支持直接通过索引器设置键值对,这是最简单的方式:

$Sample.Properties['key'] = 'value'

方法2:显式调用接口的Add方法

将对象转换为对应的接口类型后调用Add方法:

([System.Collections.Generic.IDictionary[string,string]]$Sample.Properties).Add('key','value')

或者使用键值对对象调用Add方法:

$kvp = [System.Collections.Generic.KeyValuePair[string,string]]::new('key','value')
$Sample.Properties.Add($kvp)

修改后的完整代码示例(使用索引器):

$telemetryClient = [Microsoft.ApplicationInsights.TelemetryClient]::new()
$telemetryClient.TelemetryConfiguration.ConnectionString = 'InstrumentationKey=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/'

$Sample = [Microsoft.ApplicationInsights.DataContracts.MetricTelemetry]::new()
$Sample.Name = 'TotalAppRegs'
$Sample.Timestamp = $now
$Sample.Value = 100
# 添加自定义维度
$Sample.Properties['Environment'] = 'Production'
$Sample.Properties['AppName'] = 'MyWebApp'

$telemetryClient.TrackMetric($Sample)
$telemetryClient.Flush()

内容的提问来源于stack exchange,提问作者Gert Jan Kraaijeveld

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 00:22:49