using System; using System.Collections.Generic; using System.Text; namespace Volian.Base.Library { /// /// VolianTimer Class - Times from Open to Close /// Stores the results in a static list /// public class VolianTimer { #pragma warning disable S6669 private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); #pragma warning restore S6669 [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Keeping Collections Not ReadOnly")] static List _Timers = new List(); public string Name { get; set; } public DateTime Start { get; set; } public long Ticks { get; set; } public int Count { get; set; } /// /// Constructor /// public VolianTimer() { Count = 0; Ticks = 0; } /// /// 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); } /// /// 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; } } /// /// 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(); } } }