using System; using System.Diagnostics; using Umbraco.Core.Logging; namespace Umbraco.Core { /// /// Starts the timer and invokes a callback upon disposal. Provides a simple way of timing an operation by wrapping it in a using (C#) statement. /// /// /// /// Console.WriteLine("Testing Stopwatchdisposable, should be 567:"); // using (var timer = new DisposableTimer(result => Console.WriteLine("Took {0}ms", result))) // { // Thread.Sleep(567); // } /// /// public class DisposableTimer : DisposableObject { private readonly Stopwatch _stopwatch = Stopwatch.StartNew(); private readonly Action _callback; protected DisposableTimer(Action callback) { _callback = callback; } public Stopwatch Stopwatch { get { return _stopwatch; } } /// /// Starts the timer and invokes the specified callback upon disposal. /// /// The callback. /// public static DisposableTimer Start(Action callback) { return new DisposableTimer(callback); } public static DisposableTimer TraceDuration(Func startMessage, Func completeMessage) { return TraceDuration(typeof(T), startMessage, completeMessage); } public static DisposableTimer TraceDuration(Type loggerType, Func startMessage, Func completeMessage) { LogHelper.Debug(loggerType, startMessage); return new DisposableTimer(x => LogHelper.Info(loggerType, () => completeMessage() + " (took " + x + "ms)")); } /// /// Adds a start and end log entry as Info and tracks how long it takes until disposed. /// /// /// /// /// public static DisposableTimer TraceDuration(string startMessage, string completeMessage) { return TraceDuration(typeof(T), startMessage, completeMessage); } /// /// Adds a start and end log entry as Info and tracks how long it takes until disposed. /// /// /// /// /// public static DisposableTimer TraceDuration(Type loggerType, string startMessage, string completeMessage) { LogHelper.Info(loggerType, () => startMessage); return new DisposableTimer(x => LogHelper.Info(loggerType, () => completeMessage + " (took " + x + "ms)")); } /// /// Adds a start and end log entry as Debug and tracks how long it takes until disposed. /// /// /// /// /// public static DisposableTimer DebugDuration(string startMessage, string completeMessage) { return DebugDuration(typeof(T), startMessage, completeMessage); } /// /// Adds a start and end log entry as Debug and tracks how long it takes until disposed. /// /// /// /// /// public static DisposableTimer DebugDuration(Func startMessage, Func completeMessage) { return DebugDuration(typeof(T), startMessage, completeMessage); } /// /// Adds a start and end log entry as Debug and tracks how long it takes until disposed. /// /// /// /// /// public static DisposableTimer DebugDuration(Type loggerType, string startMessage, string completeMessage) { LogHelper.Debug(loggerType, () => startMessage); return new DisposableTimer(x => LogHelper.Debug(loggerType, () => completeMessage + " (took " + x + "ms)")); } /// /// Adds a start and end log entry as Debug and tracks how long it takes until disposed. /// /// /// /// /// public static DisposableTimer DebugDuration(Type loggerType, Func startMessage, Func completeMessage) { LogHelper.Debug(loggerType, startMessage); return new DisposableTimer(x => LogHelper.Debug(loggerType, () => completeMessage() + " (took " + x + "ms)")); } /// /// Handles the disposal of resources. Derived from abstract class which handles common required locking logic. /// protected override void DisposeResources() { _callback.Invoke(Stopwatch.ElapsedMilliseconds); } } }