using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Volian.Base.Library { /// /// VolianTimer Class - Times from Open to Close /// Stores the results in a static list /// public class VolianTimer { private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); static List _Timers = new List(); /// /// User defined name - should be as specific as possible /// Include Method Name, File Name and Line Number /// private string _Name; public string Name { get { return _Name; } set { _Name = value; } } /// /// Set on open /// private DateTime _Start; public DateTime Start { get { return _Start; } set { _Start = value; } } /// /// Calculate on Close /// private long _Ticks; public long Ticks { get { return _Ticks; } set { _Ticks = value; } } /// /// Number of times open/close has been run /// private int _Count; public int Count { get { return _Count; } set { _Count = value; } } /// /// Constructor /// public VolianTimer() { Count = 0; Ticks = 0; } /// /// Command Line Parameter /Timing turns timing on /// private static bool? _TimingsOn = null; public static bool TimingsOn { get { if (_TimingsOn == null) _TimingsOn = Volian.Base.Library.VlnSettings.GetCommandFlag("Timing"); return (bool) _TimingsOn; } } /// /// Constructor with Module and line number /// /// Method Name, File Name /// Code Line Number public VolianTimer(string module, int line) { Count = 0; Ticks = 0; Name = string.Format("{0}:{1}", module, line); if(TimingsOn) _Timers.Add(this); } /// /// Start Timer /// public void Open() { Start = DateTime.Now; Count++; } /// /// Stop Timer record statistics /// public void Close() { DateTime end = DateTime.Now; TimeSpan ts = TimeSpan.FromTicks(end.Ticks - Start.Ticks); Ticks += ts.Ticks; } /// /// Show the results in the error log /// /// public static string ShowTimers() { StringBuilder sb = new StringBuilder(); if (TimingsOn) { sb.AppendLine( "===============================================\r\n" + "Count\tSeconds\tEvent\r\n" + "----------------------------------------------- "); foreach (VolianTimer tmr in _Timers) sb.AppendLine(string.Format("{0}\t{1:N1}\t{2}", tmr.Count, TimeSpan.FromTicks(tmr.Ticks).TotalSeconds, tmr.Name)); sb.AppendLine("===============================================\r\n"); _MyLog.InfoFormat(sb.ToString()); } return sb.ToString(); } } }