0

I am trying to do some logging with Application Insights and I would like to log each dependency call.

I do this based on the following SO answer on how to set header values: link

In the RequestResponseAsync method I create my DependencyTelemetry object and I am able to log the service to where the call is going to, the duration etc. However, I would also like to be able to set the method name which is called inside the service but I can't seem to find it anywhere. We are talking about WebApi to SF service calls and SF service to SF service calls.

Can someone please advise if I'm on the correct path here or wether I should be overriding some other method.

Here is my custom code based on the previously linked SO answer:

public async Task<IServiceRemotingResponseMessage> 
RequestResponseAsync(IServiceRemotingRequestMessage requestMessage)
{
    var correlationId = SetCorrelationId(requestMessage);
    IServiceRemotingResponseMessage response = null;

    var startTime = DateTime.UtcNow;
    var timer = Stopwatch.StartNew();

    try
    {
        response = await _inner.RequestResponseAsync(requestMessage);
        timer.Stop();
        return response;
    }
    finally
    {
        CreateDependencyTelemetry(correlationId, _inner.ResolvedServicePartition.ServiceName.AbsoluteUri, startTime, timer.Elapsed, response != null, _inner.ResolvedServicePartition.ServiceName.AbsoluteUri, response == null ? "400" : "200");
    }
}

private static string SetCorrelationId(IServiceRemotingRequestMessage requestMessage)
{
    string correlationId;

    if (Activity.Current != null)
        correlationId = Activity.Current.RootId;
    else
        correlationId = (string)CallContext.GetData(Constants.CorrelationId);

    requestMessage.GetHeader().AddHeader(Constants.CorrelationId, Encoding.ASCII.GetBytes(correlationId));

    return correlationId;
}

private void CreateDependencyTelemetry(string correlationId,string target, DateTime startTime, TimeSpan duration, bool success, string name, string resultCode = "200")
{
    var dependencyTelemetry = new DependencyTelemetry();
    dependencyTelemetry.Context.Operation.Id = correlationId;
    dependencyTelemetry.Type = "ServiceFabricServiceRemoting";
    dependencyTelemetry.ResultCode = resultCode; //TODO: Create better result codes?
    dependencyTelemetry.Name = name; //TODO: Provide specific method name
    dependencyTelemetry.Success = success;
    dependencyTelemetry.Duration = duration;
    dependencyTelemetry.Timestamp = startTime;
    dependencyTelemetry.Target = target;

    _telemetryClient.TrackDependency(dependencyTelemetry);
}

This generates dependency tracking logs where the name of the dependency looks like this: fabric:/ProjectName/ServiceName

And I would like to have it look like this: fabric:/ProjectName/Servicename/ServiceMethodName

I have Stateless services if that would make a difference.

Any help is much appreciated

Dimitris
  • 155
  • 1
  • 11
  • You can build this yourself or use https://github.com/Microsoft/ApplicationInsights-ServiceFabric (which is currently in beta but so far does the job well). At leat you can use it to take a look at how they do things. – Peter Bons May 17 '18 at 09:05
  • I do use this (beta) package and when I call a service fabric Service from within the WebApi I indeed get the Dependency call logged to application insights with the specific method name logged. But when I call a Service Fabric service directly from within another Service Fabric service, nothing gets logged. – Dimitris May 17 '18 at 09:32
  • 2
    Did you wire up the correct modules? See [this gist](https://gist.github.com/Expecho/8601ef27361c8df09048df118f6fad67) for an example) (Use it by calling `new ServiceMonitor().Start(Context);` in , for example, the `RunAsync`of your services. – Peter Bons May 17 '18 at 09:42
  • Thanks! That seems to work. – Dimitris May 17 '18 at 11:45

0 Answers0