Skip to content

Instantly share code, notes, and snippets.

Created September 20, 2012 21:27
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
// Sometimes it's easier to make assumptions about performance as a trade off for simplified code, but why not make it easy to notify us when that assumption is violated? Next step might be StopAndNotify
// Usage
using (new NotifyIfTakesMoreThan(TimeSpan.FromMinutes(5), "XYZ Import - checking all data instead of more complex code to optimize it"))
// Implementation
public class NotifyIfTakesMoreThan : IDisposable
private readonly TimeSpan _NotificationThreshold;
private readonly string _Description;
private readonly Stopwatch _StopWatch;
private static readonly ILog Log = LogManager.GetLogger(typeof (NotifyIfTakesMoreThan));
public NotifyIfTakesMoreThan(TimeSpan notificationThreshold, string description)
_NotificationThreshold = notificationThreshold;
_Description = description;
_StopWatch = new Stopwatch();
public void Dispose()
if (_StopWatch.Elapsed > _NotificationThreshold)
Log.Error("Operation exceeded threshold: " + new {_Description, _NotificationThreshold, _StopWatch.Elapsed});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment