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