5

Is there a way to configure App Insights to collect the operation name when monitoring a WCF service? All requests get lumped together by URL (which are just POSTs that end in .svc), so there is no easy way to determine which particular operation was called on the service.

Does there need to be a custom Telemetry Initializer that can somehow determine which operation was actually called and set a custom property? if so, how do you determine the current WCF operation name?

BrettJ
  • 980
  • 8
  • 15

3 Answers3

3

Another option for collecting data on WCF operations is to use the Microsoft.ApplicationInsights.Wcf Nuget package. You can read more about this here.

Geoff Hardy
  • 331
  • 3
  • 9
2

Brett,

Operation name can be customized in two ways:

1) Using a custom telemetry initializer - that specifically sets operation name. For more information about telemetry initializers: Custom Telemetry Initializers

2) From sdk version 2-beta3, auto-generated request telemetry is accessible though HttpContext extension method:

System.Web.HttpContextExtension.GetRequestTelemetry

Once the request telemetry is retrieved, operation name associated with it can be changed.

Please let me know if this addressed your question.

Thanks, Karthik

  • If doing #1, http://stackoverflow.com/a/26287168/3745837 shows how to get the WCF operation name (which is the last piece of the Action); I will probably go this route for now and store the Operation as a custom property via a Telemetry Initializer I might try and derive from OperationNameTelemetryInitializer and try and customize the Name there; what I really want is to simply "augment" what it calculates from the request, but append the WCF action's operation name. That way in AI portal I can specifically which operations of a WCF web service are being used, not just which web service. – BrettJ Jan 05 '16 at 17:56
  • It looks like I can do what i want with v2 SDK, because OperationNameTelemetryInitializer is no longer internal, like it is in 1.2.3. Hopefully v2 will release soon! – BrettJ Jan 05 '16 at 18:39
0

If you want to get the name of the WCF method called from a client in application insight you can use the following ITelemetryInitializer

With .net 5.0, the httprequest object is stored in the raw object properties of the telemetry context.

public class SoapActionHeaderTelemetryInitializer : ITelemetryInitializer
{

    private static readonly Regex _soapActionUri = new Regex("^\"(?<uri>.*)\"$", RegexOptions.Compiled);

    public void Initialize(ITelemetry telemetry)
    {
        DependencyTelemetry httpDependency = telemetry as DependencyTelemetry;
        if (httpDependency != null)
        {
            httpDependency.Context.TryGetRawObject("HttpRequest", out var request);
            if (request is HttpRequestMessage httpRequest)
            {
                if (httpRequest.Headers.TryGetValues("SOAPAction", out var values) && values.Any())
                {
                    // SOAP Action is contained within quote : https://www.w3.org/TR/2000/NOTE-SOAP-20000508/#_Toc478383528 
                    var soapAction = _soapActionUri.Match(values.First()).Groups["uri"].Value; 
                    telemetry.Context.GlobalProperties["SOAPAction"] = soapAction;
                }
            }
        }
    }
}

enter image description here

Cyril Durand
  • 15,834
  • 5
  • 54
  • 62