I applied Application Insights in an MVC .NET project. The solution has 3 projects: the MVC project for the front-end, and 2 .NET Library Class projects, one for the business layer, and the other for the data access layer.
By default, Application Insights automatically track HTTP requests and the SQL call, and the business logic layer is skipped.
I tried to use TrackDependency to get metrics from the intermediate Library Class that have all the business logic of the application, and it seems to work.
What I did was implement the following code in the Controller, just when it consumes the business layer method:
LogicLayer.LogicLayer logObj = new LogicLayer.LogicLayer();
TelemetryClient telemetry = new TelemetryClient();
string stResponse = "";
var success = false;
var startTime = DateTime.UtcNow;
var timer = System.Diagnostics.Stopwatch.StartNew();
try
{
stResponse = logObj.getData();
success = true;
}
catch (Exception ex)
{
success = false;
telemetry.TrackException(ex);
throw new Exception("Operation went wrong", ex);
}
finally
{
timer.Stop();
telemetry.TrackDependency("DependencyType", "myDependency", "myCall", "", startTime, timer.Elapsed, "", success);
}
My question is: Using TrackDependency is the right way to get metrics on the intermediate Library Class that have all the business logic of the application ??? or is there another way ??
Yes, TrackDependency
method should be the easiest way to do that.
Another solution is a little more complexed, you should take use of methods in Application Insights API for custom events and metrics.
Hope it helps.