Mike
Mike

Reputation: 834

Add Custom Properties to Application Insights Request Telemetry in WCF

I'm trying to get some additional information about the message we received (basically its application-level outcome) into the Application Insights RequestTelemetry object for a WCF service.

Application Insights is logging request telemetry already. I created an ITelemetryInitializer that is being run, but at the time it runs I have no way that I can find to access information about the request, much less application-specific data from the request's context.

Is there somewhere I can put data that will be accessible by the ITelemetryInitializer at the time it runs?

    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
            }
        }
    }

Upvotes: 2

Views: 3898

Answers (3)

Rory
Rory

Reputation: 41887

In case it helps anyone, Application Insights automatically adds custom dimensions from data you store in System.Diagnostics.Activity.Current.AddBaggage(), or at least it does in asp.net 5. That might be available at the right place for you in WCF land.

e.g.

var currentActivity = System.Diagnostics.Activity.Current;
if (currentActivity != null)
{
    currentActivity.AddBaggage("MyPropertyName", someData);
}

Upvotes: 2

Abhishek Anand
Abhishek Anand

Reputation: 176

I had to face similar issue as the author described. Tried by implementing ITelemetryInitializer/ITelemetryProcessor but did not work.

Ended up writing my own MessageTraceTelemetryModule class implementing IWcfTelemetryModule and IWcfMessageTrace.

In the OnTraceResponse method, I added my custom property to the request telemetry by extracting value from OperationContext (which is accessible here!):

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());
        }
    }
}

New custom property visible in Application Insights telemetry - ClientID custom property Pic.
Note that the clientID property is being set in the OperationContext in Message Inspector:

public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
    if(!OperationContext.Current.IncomingMessageProperties.ContainsKey("clientID"))
        OperationContext.Current.IncomingMessageProperties.Add("clientID", clientID);
}

Brief Context:
I implemented AAD Token based Authentication in a SOAP based WCF Service.
I needed to store the clientID from the token (which is validated in message inspector) and add the same as a custom property in the application insights request telemetry.

References:

  1. Message Inspectors Documentation
  2. Application Insights for WCF Documentation

Upvotes: 2

Indar
Indar

Reputation: 305

To log custom property, you can try like this...

public class CustomTelemetry : ITelemetryInitializer
{
    public void Initialize(ITelemetry telemetry)
    {
        var requestTelemetry = telemetry as RequestTelemetry;
        if (requestTelemetry == null) return;
        requestTelemetry.Properties.Add("CustomProperty", "DummyValue");
    }
}

And register CustomTelemetry at start of the application

TelemetryConfiguration.Active.TelemetryInitializers.Add(new CustomTelemetry());

Here is the Original Answer

MS Doc - Application Insights API for custom events and metrics

Upvotes: -1

Related Questions