< Summary - Helper.Tests

Information
Class: Logging.Helper.LoggerEvent
Assembly: Logging.Helper
File(s): D:\a\NuGetPackages\NuGetPackages\src\Helper\Logging.Helper\LoggerEvent.cs
Tag: 3_8508158812
Line coverage
100%
Covered lines: 50
Uncovered lines: 0
Coverable lines: 50
Total lines: 139
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
RaiseLogEvent(...)100%44100%
LogStatus(...)100%11100%
.ctor()100%11100%
FormatMessage(...)100%11100%
FormatTimeSpan()100%11100%
ResetTimer(...)100%11100%
RestartTimer(...)100%44100%
StartTimer(...)100%22100%
StopTimer(...)100%11100%

File(s)

D:\a\NuGetPackages\NuGetPackages\src\Helper\Logging.Helper\LoggerEvent.cs

#LineLine coverage
 1using System;
 2using System.Diagnostics;
 3
 4namespace 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 )
 1037    {
 38      // Check required parameter
 1039      if( null == message )
 140      {
 141        message = string.Empty;
 142      }
 43
 1044      var handler = RaiseLogHandler;
 1045      if( null != handler )
 146      {
 47        // Raise the log event if a handler has been set
 148        handler.Invoke( this, new LoggerEventArgs( message, severity ) );
 149      }
 50      else
 951      {
 52        // Display the message when a logger has not been set
 953        Console.WriteLine( severity + @": " + message );
 954      }
 1055    }
 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 )
 161    {
 162      RaiseLogEvent( string.Format( message, args ) );
 163    }
 64
 65    #endregion
 66
 67    #region Operations Timing
 68
 1169    private readonly Stopwatch _sw = new Stopwatch();
 70
 71    private static string FormatMessage( string msg )
 472    {
 473      return $"Timing.: {msg.Trim()}";
 474    }
 75
 76    private string FormatTimeSpan()
 377    {
 378      return $"Elapsed: {_sw.Elapsed}";
 379    }
 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 )
 184    {
 85      // Log the elapse time
 186      RaiseLogEvent( FormatTimeSpan(), severity );
 87
 188      _sw.Reset();
 189    }
 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 )
 197    {
 98      // Log the elapse time
 199      if( _sw.IsRunning )
 1100      {
 1101        RaiseLogEvent( FormatTimeSpan(), severity );
 1102      }
 103
 104      // Log the start message
 1105      if( !string.IsNullOrEmpty( message ) )
 1106      {
 1107        RaiseLogEvent( FormatMessage( message ), severity );
 1108      }
 109
 1110      _sw.Restart();
 1111    }
 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 )
 3117    {
 118      // Log the start message
 3119      if( !string.IsNullOrWhiteSpace( message ) )
 3120      {
 3121        RaiseLogEvent( FormatMessage( message ), severity );
 3122      }
 123
 3124      _sw.Start();
 3125    }
 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 )
 1130    {
 1131      _sw.Stop();
 132
 133      // Log the elapse time
 1134      RaiseLogEvent( FormatTimeSpan(), severity );
 1135    }
 136
 137    #endregion
 138  }
 139}