< Summary - Helper.Tests

Information
Class: Logging.Helper.ImplNLog
Assembly: Logging.Helper
File(s): D:\a\NuGetPackages\NuGetPackages\src\Helper\Logging.Helper\ImplNLog.cs
Tag: 3_8508158812
Line coverage
100%
Covered lines: 65
Uncovered lines: 0
Coverable lines: 65
Total lines: 188
Line coverage: 100%
Branch coverage
91%
Covered branches: 11
Total branches: 12
Branch coverage: 91.6%
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
get_MaxLogFiles()100%11100%
get_IsDebugEnabled()100%11100%
get_IsErrorEnabled()100%11100%
get_IsFatalEnabled()100%11100%
get_IsInfoEnabled()100%11100%
get_IsWarnEnabled()100%11100%
Debug(...)100%11100%
Error(...)100%11100%
Error(...)100%11100%
Error(...)100%11100%
Fatal(...)100%11100%
Info(...)100%11100%
Warn(...)100%11100%
SetLogFile(...)100%44100%
.ctor(...)87.5%88100%
Finalize()100%11100%
GetDefaultConfig()100%11100%

File(s)

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

#LineLine coverage
 1#region NLog Information
 2// NLog Home
 3// http://nlog-project.org/
 4
 5// NLog Documentation
 6// https://github.com/nlog/nlog/wiki
 7
 8// NLog Tutorials
 9// https://github.com/NLog/NLog/wiki/Tutorial
 10// https://www.codeproject.com/Articles/10631/Introduction-to-NLog
 11#endregion
 12
 13using System;
 14using NLog.Config;
 15
 16namespace Logging.Helper
 17{
 18  /// <summary>Class that implements NLog logging.</summary>
 19  internal class ImplNLog : ILog
 20  {
 21    #region Constants
 22
 23    private const string cDefaultPattern = @"${longdate} ${uppercase:${level:padding=-5}} ${message}";
 24    private const string cLogFileDirVar = @"basedir";
 25    private const string cLogFileNameVar = @"logfile";
 26
 27    #endregion
 28
 29    #region ILog Implementation
 30
 31    #region Properties
 32
 33    /// <inheritdoc/>
 1734    public int MaxLogFiles { get; set; }
 35
 36    /// <inheritdoc/>
 5337    public bool IsDebugEnabled => _logger.IsDebugEnabled;
 38
 39    /// <inheritdoc/>
 7440    public bool IsErrorEnabled => _logger.IsErrorEnabled;
 41
 42    /// <inheritdoc/>
 3943    public bool IsFatalEnabled => _logger.IsFatalEnabled;
 44
 45    /// <inheritdoc/>
 5446    public bool IsInfoEnabled => _logger.IsInfoEnabled;
 47
 48    /// <inheritdoc/>
 4249    public bool IsWarnEnabled => _logger.IsWarnEnabled;
 50
 51    #endregion
 52
 53    #region Methods
 54
 55    /// <inheritdoc/>
 56    public void Debug( string message )
 657    {
 658      _logger.Debug( message );
 659    }
 60
 61    /// <inheritdoc/>
 62    public void Error( string message )
 363    {
 364      _logger.Error( message );
 365    }
 66
 67    /// <inheritdoc/>
 68    /// <remarks>
 69    /// You need to use an exception rendering option such as
 70    /// "${exception:format=toString,Data:maxInnerExceptionLevel=10}"
 71    /// in your target layout to actually capture the exception information.
 72    /// </remarks>
 73    public void Error( string message, Exception exception )
 274    {
 275      _logger.Error( exception, message );
 276    }
 77
 78    /// <inheritdoc/>
 79    /// <remarks>
 80    /// You need to use an exception rendering option such as
 81    /// "${exception:format=toString,Data:maxInnerExceptionLevel=10}"
 82    /// in your target layout to actually capture the exception information.
 83    /// </remarks>
 84    public void Error( Exception exception )
 285    {
 286      _logger.Error( exception );
 287    }
 88
 89    /// <inheritdoc/>
 90    public void Fatal( string message )
 491    {
 492      _logger.Fatal( message );
 493    }
 94
 95    /// <inheritdoc/>
 96    public void Info( string message )
 697    {
 698      _logger.Info( message );
 699    }
 100
 101    /// <inheritdoc/>
 102    public void Warn( string message )
 7103    {
 7104      _logger.Warn( message );
 7105    }
 106
 107    /// <inheritdoc/>
 108    public void SetLogFile( string logDirectory, string logFileName = "" )
 1109    {
 1110      if( !string.IsNullOrWhiteSpace( logDirectory ) )
 1111      {
 112        // Change the NLog variable used for the log file folder
 1113        NLog.LogManager.Configuration.Variables[cLogFileDirVar] = logDirectory;
 1114      }
 115
 1116      if( !string.IsNullOrWhiteSpace( logFileName ) )
 1117      {
 118        // Change the NLog variable used for the log file name
 1119        NLog.LogManager.Configuration.Variables[cLogFileNameVar] = logFileName;
 1120      }
 1121    }
 122
 123    #endregion
 124
 125    #endregion
 126
 127    #region Constructor / Destructor
 128
 129    private readonly NLog.Logger _logger;
 130
 131    /// <summary>Creates a new instance of the ImplNLog class.</summary>
 132    /// <param name="loggerType">Type to be used as the name of the logger.</param>
 133    /// <param name="configFile">NLog configuration file to use.</param>
 43134    internal ImplNLog( Type loggerType, string configFile )
 43135    {
 43136      if( !string.IsNullOrWhiteSpace( configFile ) )
 41137      {
 138        // Set the configuration from a file
 41139        NLog.LogManager.ThrowConfigExceptions = true;
 41140        NLog.LogManager.Configuration = new XmlLoggingConfiguration( configFile );
 41141      }
 142
 43143      if( null == NLog.LogManager.Configuration || // No configuration file
 43144        NLog.LogManager.Configuration.ConfiguredNamedTargets.Count == 0 ) // No targets loaded
 1145      {
 146        // Set a default configuration
 1147        NLog.LogManager.Configuration = GetDefaultConfig();
 1148      }
 149
 150      // Set the logger
 43151      _logger = null != loggerType ?
 43152        NLog.LogManager.GetLogger( loggerType.FullName ) :
 43153        NLog.LogManager.GetCurrentClassLogger();
 43154    }
 155
 156    /// <summary>Called once the object has been disposed.</summary>
 157    ~ImplNLog()
 54158    {
 27159      NLog.LogManager.Shutdown(); // Flush and close down internal threads and timers
 54160    }
 161
 162    #endregion
 163
 164    #region Private Methods
 165
 166    /// <summary>Gets a default console configuration.</summary>
 167    private static LoggingConfiguration GetDefaultConfig()
 1168    {
 169      // Create configuration object
 1170      LoggingConfiguration retValue = new LoggingConfiguration();
 171
 172      // Create target and add it to the configuration
 1173      NLog.Targets.ConsoleTarget consoleTarget = new NLog.Targets.ConsoleTarget();
 1174      retValue.AddTarget( "console", consoleTarget );
 175
 176      // Set target properties
 1177      consoleTarget.Layout = cDefaultPattern;
 178
 179      // Define rules
 1180      LoggingRule rule = new LoggingRule( "*", NLog.LogLevel.Debug, consoleTarget );
 1181      retValue.LoggingRules.Add( rule );
 182
 1183      return retValue;
 1184    }
 185
 186    #endregion
 187  }
 188}