Azure 向WCF中的Application Insights请求遥测添加自定义属性
我试图将我们收到的消息(基本上是它的应用程序级结果)的一些附加信息输入到WCF服务的application InsightsAzure 向WCF中的Application Insights请求遥测添加自定义属性,azure,wcf,azure-application-insights,Azure,Wcf,Azure Application Insights,我试图将我们收到的消息(基本上是它的应用程序级结果)的一些附加信息输入到WCF服务的application InsightsRequestTelemetry对象中 Application Insights已在记录请求遥测。我创建了一个正在运行的ITelemetryInitializer,但在它运行时,我无法找到访问请求信息的方法,更不用说请求上下文中特定于应用程序的数据了 是否有地方可以放置运行时可由ITelemetryInitializer访问的数据 public class WcfS
RequestTelemetry
对象中
Application Insights已在记录请求遥测。我创建了一个正在运行的ITelemetryInitializer
,但在它运行时,我无法找到访问请求信息的方法,更不用说请求上下文中特定于应用程序的数据了
是否有地方可以放置运行时可由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());
原来的答案是什么
-自定义事件和度量的Application Insights API我不得不面对作者描述的类似问题。通过实现
ITelemetryInitializer
/ITelemetryProcessor
进行了尝试,但无效
最后编写了自己的MessageTraceTelemetryModule
类,实现了iwcftremoterymodule
和iwcftmessagetrace
在ontaceresponse
方法中,我通过从OperationContext
中提取值(可在此处访问!),将自定义属性添加到请求遥测中:
内部类MessageTraceTelemetryModule:IWcfTelemetryModule,IWcfMessageTrace
{
公共无效OnTraceResponse(IOperationContext操作,ref消息响应)
{
if(OperationContext.Current.IncomingMessageProperties.TryGetValue(“clientID”,out对象值))
{
operation.Request.Properties.Add(“clientID”,value.ToString());
}
}
}
新的自定义属性在Application Insights telemetry中可见-。请注意,正在消息检查器中的
OperationContext
中设置clientID
属性:
接收方请求后的公共对象(ref消息请求、IClientChannel通道、InstanceContext InstanceContext)
{
如果(!OperationContext.Current.IncomingMessageProperties.ContainsKey(“clientID”))
OperationContext.Current.IncomingMessageProperties.Add(“clientID”,clientID);
}
简要内容:
我在基于SOAP的WCF服务中实现了基于AAD令牌的身份验证
我需要存储令牌中的
clientID
(在message inspector中验证),并将其作为自定义属性添加到application insights request telemetry中。
参考文献:
希望这能回答您的问题,如果您需要更多详细信息,请告诉我李>
您要添加到遥测中的数据是什么?请提供一个例子。例如,我有基本的身份验证。从授权标题中的用户名,我在系统中查找用户记录。我想在遥测系统中存储用户的ID。此外,这个服务是一个如果有应用程序错误,它返回一个200,错误描述在响应文本中,如果可能的话,我想把错误代码放在遥测中。我在尝试类似的东西:HttpContext.Current.Items[“property”]=“value”
但是HttpContext.Current
在应用程序代码中不可用。我还尝试添加到OperationContext.Current.IncomingRequestProperties
,该属性可以从应用程序中获得,但不能从TelemetryInitializer
中获得,很遗憾我已经放弃了。我的解决方案是将服务迁移到WebAPI,老实说,这可能是为了我的长期理智。这并不能回答问题。看起来OP知道如何添加属性,但需要一种方法来访问有关WCF操作的详细信息,以用作自定义属性的值。如Peter所说,我知道如何添加属性。如果我只是想为每个请求添加一个常量值,我会很好,但我正在尝试获取其中的一些信息,这些信息不仅针对请求,而且针对我的应用程序如何处理请求。我希望从我的应用程序代码中调用HttpContext.Current.Items
,但它是空的。