Skip to content

Instantly share code, notes, and snippets.

@taddison
Created February 23, 2017 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taddison/81f247cb67c2608206acccf7249c2dea to your computer and use it in GitHub Desktop.
Save taddison/81f247cb67c2608206acccf7249c2dea to your computer and use it in GitHub Desktop.
Extension method to time and track a dependency
using Microsoft.ApplicationInsights;
using System;
public static class TelemetryClientExtensions
{
public static TResult TrackDependencyTiming<TResult>(this TelemetryClient client, string dependencyName, string commandName, Func<TResult> function)
{
var startTime = DateTime.UtcNow;
var timer = System.Diagnostics.Stopwatch.StartNew();
var success = true;
try
{
return function();
}
catch
{
success = false;
throw;
}
finally
{
timer.Stop();
client.TrackDependency(dependencyName, commandName, startTime, timer.Elapsed, success);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment