0
votes

I have a queue listener which makes multiple REST API calls once it receives a message. I want to track those API calls as dependencies using Application Insights. Is there any way of doing it automatically like it happens for ASP.NET Core projects?

I tried something similar to the following code but it does not log the dependencies automatically.

using (var operation = ApplicationLogging.TelemetryClient.StartOperation<RequestTelemetry>("Test"))
{
    //Rest Calls
}
1

1 Answers

2
votes

The standard dependency-tracking module automatically discovers external dependencies such as databases and REST APIs. But you might want some additional components to be treated in the same way. You can write code that sends dependency information, using the same TrackDependency API that is used by the standard modules.

Use the TrackDependency call to track the response times and success rates of calls to an external piece of code. The results appear in the dependency charts in the portal.

Snippet in C#

var success = false;
var startTime = DateTime.UtcNow;
var timer = System.Diagnostics.Stopwatch.StartNew();
try
{
success = dependency.Call();
}
finally
{
timer.Stop();
telemetry.TrackDependency("myDependency", "myCall", startTime, timer.Elapsed,    success);
// With the Latest SDK follow the below format:
// TrackDependency (string dependencyTypeName, string dependencyName, string data, DateTimeOffset startTime, TimeSpan duration, bool success);
}

Documentation Reference

Hope this helps.