将自定义属性添加到 WCF 中的 Application Insights 请求遥测
Add Custom Properties to Application Insights Request Telemetry in WCF
我正在尝试将有关我们收到的消息(基本上是其应用程序级结果)的一些其他信息获取到 WCF 服务的 Application Insights RequestTelemetry
对象中。
Application Insights 已经在记录请求遥测。我创建了一个 ITelemetryInitializer
,它是 运行,但当时它 运行s 我无法找到访问有关请求的信息,更不用说来自请求的上下文。
有什么地方可以放置 ITelemetryInitializer
在 运行 时可以访问的数据吗?
public class WcfServiceTelemetryInitializer : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
if (telemetry is RequestTelemetry rTelemetry)
{
// HttpContext.Current is populated at this point, but doesn't seem to be available within my application code.
// So is System.ServiceModel.OperationContext.Current
}
}
}
要记录自定义 属性,您可以这样尝试...
public class CustomTelemetry : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
var requestTelemetry = telemetry as RequestTelemetry;
if (requestTelemetry == null) return;
requestTelemetry.Properties.Add("CustomProperty", "DummyValue");
}
}
并在申请开始时注册CustomTelemetry
TelemetryConfiguration.Active.TelemetryInitializers.Add(new CustomTelemetry());
Here为原答案
MS Doc - Application Insights API 用于自定义事件和指标
我不得不面对作者描述的类似问题。尝试通过实施 ITelemetryInitializer
/ITelemetryProcessor
但没有奏效。
最终编写了我自己的 MessageTraceTelemetryModule
class 实现 IWcfTelemetryModule
和 IWcfMessageTrace
.
在 OnTraceResponse
方法中,我通过从 OperationContext
中提取值将我的自定义 属性 添加到请求遥测中( 可在此处访问!):
internal class MessageTraceTelemetryModule : IWcfTelemetryModule, IWcfMessageTrace
{
public void OnTraceResponse(IOperationContext operation, ref Message response)
{
if (OperationContext.Current.IncomingMessageProperties.TryGetValue("clientID", out object value))
{
operation.Request.Properties.Add("clientID", value.ToString());
}
}
}
新自定义 属性 在 Application Insights 遥测中可见 - ClientID custom property Pic。
请注意,clientID
属性 正在 Message Inspector 的 OperationContext
中设置:
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
if(!OperationContext.Current.IncomingMessageProperties.ContainsKey("clientID"))
OperationContext.Current.IncomingMessageProperties.Add("clientID", clientID);
}
简要背景:
我在 基于 SOAP 的 WCF 服务 中实现了基于 AAD 令牌的身份验证。
我需要存储令牌中的 clientID
(在消息检查器中验证)并在应用程序洞察请求遥测中添加与 custom property
相同的内容。
参考文献:
如果它对任何人有帮助,Application Insights 会自动从您存储在 System.Diagnostics.Activity.Current.AddBaggage()
中的数据中添加自定义维度,或者至少它会在 asp.net 中这样做 5. 这可能在正确的位置可用你在 WCF 土地上。
例如
var currentActivity = System.Diagnostics.Activity.Current;
if (currentActivity != null)
{
currentActivity.AddBaggage("MyPropertyName", someData);
}
我正在尝试将有关我们收到的消息(基本上是其应用程序级结果)的一些其他信息获取到 WCF 服务的 Application Insights RequestTelemetry
对象中。
Application Insights 已经在记录请求遥测。我创建了一个 ITelemetryInitializer
,它是 运行,但当时它 运行s 我无法找到访问有关请求的信息,更不用说来自请求的上下文。
有什么地方可以放置 ITelemetryInitializer
在 运行 时可以访问的数据吗?
public class WcfServiceTelemetryInitializer : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
if (telemetry is RequestTelemetry rTelemetry)
{
// HttpContext.Current is populated at this point, but doesn't seem to be available within my application code.
// So is System.ServiceModel.OperationContext.Current
}
}
}
要记录自定义 属性,您可以这样尝试...
public class CustomTelemetry : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
var requestTelemetry = telemetry as RequestTelemetry;
if (requestTelemetry == null) return;
requestTelemetry.Properties.Add("CustomProperty", "DummyValue");
}
}
并在申请开始时注册CustomTelemetry
TelemetryConfiguration.Active.TelemetryInitializers.Add(new CustomTelemetry());
Here为原答案
MS Doc - Application Insights API 用于自定义事件和指标
我不得不面对作者描述的类似问题。尝试通过实施 ITelemetryInitializer
/ITelemetryProcessor
但没有奏效。
最终编写了我自己的 MessageTraceTelemetryModule
class 实现 IWcfTelemetryModule
和 IWcfMessageTrace
.
在 OnTraceResponse
方法中,我通过从 OperationContext
中提取值将我的自定义 属性 添加到请求遥测中( 可在此处访问!):
internal class MessageTraceTelemetryModule : IWcfTelemetryModule, IWcfMessageTrace
{
public void OnTraceResponse(IOperationContext operation, ref Message response)
{
if (OperationContext.Current.IncomingMessageProperties.TryGetValue("clientID", out object value))
{
operation.Request.Properties.Add("clientID", value.ToString());
}
}
}
新自定义 属性 在 Application Insights 遥测中可见 - ClientID custom property Pic。
请注意,clientID
属性 正在 Message Inspector 的 OperationContext
中设置:
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
if(!OperationContext.Current.IncomingMessageProperties.ContainsKey("clientID"))
OperationContext.Current.IncomingMessageProperties.Add("clientID", clientID);
}
简要背景:
我在 基于 SOAP 的 WCF 服务 中实现了基于 AAD 令牌的身份验证。
我需要存储令牌中的 clientID
(在消息检查器中验证)并在应用程序洞察请求遥测中添加与 custom property
相同的内容。
参考文献:
如果它对任何人有帮助,Application Insights 会自动从您存储在 System.Diagnostics.Activity.Current.AddBaggage()
中的数据中添加自定义维度,或者至少它会在 asp.net 中这样做 5. 这可能在正确的位置可用你在 WCF 土地上。
例如
var currentActivity = System.Diagnostics.Activity.Current;
if (currentActivity != null)
{
currentActivity.AddBaggage("MyPropertyName", someData);
}