| | 1 | | using System; |
| | 2 | | using System.Diagnostics; |
| | 3 | |
|
| | 4 | | namespace Logging.Helper |
| | 5 | | { |
| | 6 | | #region Log Severity types |
| | 7 | |
|
| | 8 | | /// <summary>Logging severity types.</summary> |
| | 9 | | public enum LogSeverity |
| | 10 | | { |
| | 11 | | /// <summary>Log as a debugging message.</summary> |
| | 12 | | Debug = 0, |
| | 13 | | /// <summary>Log as an informational message.</summary> |
| | 14 | | Information = 1, |
| | 15 | | /// <summary>Log as a warning message.</summary> |
| | 16 | | Warning = 2, |
| | 17 | | /// <summary>Log as an error message.</summary> |
| | 18 | | Error = 3, |
| | 19 | | /// <summary>Log as a fatal message.</summary> |
| | 20 | | Fatal = 4 |
| | 21 | | } |
| | 22 | |
|
| | 23 | | #endregion |
| | 24 | |
|
| | 25 | | /// <summary>Base class to handle logging using an event handler.</summary> |
| | 26 | | public class LoggerEvent |
| | 27 | | { |
| | 28 | | /// <summary>Logging event handler.</summary> |
| | 29 | | public event EventHandler<LoggerEventArgs> RaiseLogHandler; |
| | 30 | |
|
| | 31 | | #region Public Methods |
| | 32 | |
|
| | 33 | | /// <summary>Logs a message by using a RaiseLogHandler event.</summary> |
| | 34 | | /// <param name="message">Message text.</param> |
| | 35 | | /// <param name="severity">Identifies the type of trace event.</param> |
| | 36 | | public virtual void RaiseLogEvent( string message, LogSeverity severity = LogSeverity.Information ) |
| 10 | 37 | | { |
| | 38 | | // Check required parameter |
| 10 | 39 | | if( null == message ) |
| 1 | 40 | | { |
| 1 | 41 | | message = string.Empty; |
| 1 | 42 | | } |
| | 43 | |
|
| 10 | 44 | | var handler = RaiseLogHandler; |
| 10 | 45 | | if( null != handler ) |
| 1 | 46 | | { |
| | 47 | | // Raise the log event if a handler has been set |
| 1 | 48 | | handler.Invoke( this, new LoggerEventArgs( message, severity ) ); |
| 1 | 49 | | } |
| | 50 | | else |
| 9 | 51 | | { |
| | 52 | | // Display the message when a logger has not been set |
| 9 | 53 | | Console.WriteLine( severity + @": " + message ); |
| 9 | 54 | | } |
| 10 | 55 | | } |
| | 56 | |
|
| | 57 | | /// <summary>Logs a status message by using a RaiseLogHandler event.</summary> |
| | 58 | | /// <param name="message">Message text.</param> |
| | 59 | | /// <param name="args">Formatting values.</param> |
| | 60 | | public virtual void LogStatus( string message, params object[] args ) |
| 1 | 61 | | { |
| 1 | 62 | | RaiseLogEvent( string.Format( message, args ) ); |
| 1 | 63 | | } |
| | 64 | |
|
| | 65 | | #endregion |
| | 66 | |
|
| | 67 | | #region Operations Timing |
| | 68 | |
|
| 11 | 69 | | private readonly Stopwatch _sw = new Stopwatch(); |
| | 70 | |
|
| | 71 | | private static string FormatMessage( string msg ) |
| 4 | 72 | | { |
| 4 | 73 | | return $"Timing.: {msg.Trim()}"; |
| 4 | 74 | | } |
| | 75 | |
|
| | 76 | | private string FormatTimeSpan() |
| 3 | 77 | | { |
| 3 | 78 | | return $"Elapsed: {_sw.Elapsed}"; |
| 3 | 79 | | } |
| | 80 | |
|
| | 81 | | /// <summary>Stops time interval measurement and resets the elapsed time to zero.</summary> |
| | 82 | | /// <param name="severity">Identifies the type of trace event. The default is debug.</param> |
| | 83 | | public virtual void ResetTimer( LogSeverity severity = LogSeverity.Debug ) |
| 1 | 84 | | { |
| | 85 | | // Log the elapse time |
| 1 | 86 | | RaiseLogEvent( FormatTimeSpan(), severity ); |
| | 87 | |
|
| 1 | 88 | | _sw.Reset(); |
| 1 | 89 | | } |
| | 90 | |
|
| | 91 | | /// <summary> |
| | 92 | | /// Stops time interval measurement, resets the elapsed time to zero, and starts measuring elapsed time. |
| | 93 | | /// </summary> |
| | 94 | | /// <param name="message">Message text.</param> |
| | 95 | | /// <param name="severity">Identifies the type of trace event. The default is debug.</param> |
| | 96 | | public virtual void RestartTimer( string message = null, LogSeverity severity = LogSeverity.Debug ) |
| 1 | 97 | | { |
| | 98 | | // Log the elapse time |
| 1 | 99 | | if( _sw.IsRunning ) |
| 1 | 100 | | { |
| 1 | 101 | | RaiseLogEvent( FormatTimeSpan(), severity ); |
| 1 | 102 | | } |
| | 103 | |
|
| | 104 | | // Log the start message |
| 1 | 105 | | if( !string.IsNullOrEmpty( message ) ) |
| 1 | 106 | | { |
| 1 | 107 | | RaiseLogEvent( FormatMessage( message ), severity ); |
| 1 | 108 | | } |
| | 109 | |
|
| 1 | 110 | | _sw.Restart(); |
| 1 | 111 | | } |
| | 112 | |
|
| | 113 | | /// <summary>Starts, or resumes, measuring elapsed time for an interval.</summary> |
| | 114 | | /// <param name="message">Message text.</param> |
| | 115 | | /// <param name="severity">Identifies the type of trace event. The default is debug.</param> |
| | 116 | | public virtual void StartTimer( string message = null, LogSeverity severity = LogSeverity.Debug ) |
| 3 | 117 | | { |
| | 118 | | // Log the start message |
| 3 | 119 | | if( !string.IsNullOrWhiteSpace( message ) ) |
| 3 | 120 | | { |
| 3 | 121 | | RaiseLogEvent( FormatMessage( message ), severity ); |
| 3 | 122 | | } |
| | 123 | |
|
| 3 | 124 | | _sw.Start(); |
| 3 | 125 | | } |
| | 126 | |
|
| | 127 | | /// <summary>Stops measuring elapsed time for an interval.</summary> |
| | 128 | | /// <param name="severity">Identifies the type of trace event. The default is debug.</param> |
| | 129 | | public virtual void StopTimer( LogSeverity severity = LogSeverity.Debug ) |
| 1 | 130 | | { |
| 1 | 131 | | _sw.Stop(); |
| | 132 | |
|
| | 133 | | // Log the elapse time |
| 1 | 134 | | RaiseLogEvent( FormatTimeSpan(), severity ); |
| 1 | 135 | | } |
| | 136 | |
|
| | 137 | | #endregion |
| | 138 | | } |
| | 139 | | } |