Created
February 23, 2017 21:11
-
-
Save taddison/81f247cb67c2608206acccf7249c2dea to your computer and use it in GitHub Desktop.
Extension method to time and track a dependency
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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