| | 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 | |
|
| | 13 | | using System; |
| | 14 | | using NLog.Config; |
| | 15 | |
|
| | 16 | | namespace 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/> |
| 17 | 34 | | public int MaxLogFiles { get; set; } |
| | 35 | |
|
| | 36 | | /// <inheritdoc/> |
| 53 | 37 | | public bool IsDebugEnabled => _logger.IsDebugEnabled; |
| | 38 | |
|
| | 39 | | /// <inheritdoc/> |
| 74 | 40 | | public bool IsErrorEnabled => _logger.IsErrorEnabled; |
| | 41 | |
|
| | 42 | | /// <inheritdoc/> |
| 39 | 43 | | public bool IsFatalEnabled => _logger.IsFatalEnabled; |
| | 44 | |
|
| | 45 | | /// <inheritdoc/> |
| 54 | 46 | | public bool IsInfoEnabled => _logger.IsInfoEnabled; |
| | 47 | |
|
| | 48 | | /// <inheritdoc/> |
| 42 | 49 | | public bool IsWarnEnabled => _logger.IsWarnEnabled; |
| | 50 | |
|
| | 51 | | #endregion |
| | 52 | |
|
| | 53 | | #region Methods |
| | 54 | |
|
| | 55 | | /// <inheritdoc/> |
| | 56 | | public void Debug( string message ) |
| 6 | 57 | | { |
| 6 | 58 | | _logger.Debug( message ); |
| 6 | 59 | | } |
| | 60 | |
|
| | 61 | | /// <inheritdoc/> |
| | 62 | | public void Error( string message ) |
| 3 | 63 | | { |
| 3 | 64 | | _logger.Error( message ); |
| 3 | 65 | | } |
| | 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 ) |
| 2 | 74 | | { |
| 2 | 75 | | _logger.Error( exception, message ); |
| 2 | 76 | | } |
| | 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 ) |
| 2 | 85 | | { |
| 2 | 86 | | _logger.Error( exception ); |
| 2 | 87 | | } |
| | 88 | |
|
| | 89 | | /// <inheritdoc/> |
| | 90 | | public void Fatal( string message ) |
| 4 | 91 | | { |
| 4 | 92 | | _logger.Fatal( message ); |
| 4 | 93 | | } |
| | 94 | |
|
| | 95 | | /// <inheritdoc/> |
| | 96 | | public void Info( string message ) |
| 6 | 97 | | { |
| 6 | 98 | | _logger.Info( message ); |
| 6 | 99 | | } |
| | 100 | |
|
| | 101 | | /// <inheritdoc/> |
| | 102 | | public void Warn( string message ) |
| 7 | 103 | | { |
| 7 | 104 | | _logger.Warn( message ); |
| 7 | 105 | | } |
| | 106 | |
|
| | 107 | | /// <inheritdoc/> |
| | 108 | | public void SetLogFile( string logDirectory, string logFileName = "" ) |
| 1 | 109 | | { |
| 1 | 110 | | if( !string.IsNullOrWhiteSpace( logDirectory ) ) |
| 1 | 111 | | { |
| | 112 | | // Change the NLog variable used for the log file folder |
| 1 | 113 | | NLog.LogManager.Configuration.Variables[cLogFileDirVar] = logDirectory; |
| 1 | 114 | | } |
| | 115 | |
|
| 1 | 116 | | if( !string.IsNullOrWhiteSpace( logFileName ) ) |
| 1 | 117 | | { |
| | 118 | | // Change the NLog variable used for the log file name |
| 1 | 119 | | NLog.LogManager.Configuration.Variables[cLogFileNameVar] = logFileName; |
| 1 | 120 | | } |
| 1 | 121 | | } |
| | 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> |
| 43 | 134 | | internal ImplNLog( Type loggerType, string configFile ) |
| 43 | 135 | | { |
| 43 | 136 | | if( !string.IsNullOrWhiteSpace( configFile ) ) |
| 41 | 137 | | { |
| | 138 | | // Set the configuration from a file |
| 41 | 139 | | NLog.LogManager.ThrowConfigExceptions = true; |
| 41 | 140 | | NLog.LogManager.Configuration = new XmlLoggingConfiguration( configFile ); |
| 41 | 141 | | } |
| | 142 | |
|
| 43 | 143 | | if( null == NLog.LogManager.Configuration || // No configuration file |
| 43 | 144 | | NLog.LogManager.Configuration.ConfiguredNamedTargets.Count == 0 ) // No targets loaded |
| 1 | 145 | | { |
| | 146 | | // Set a default configuration |
| 1 | 147 | | NLog.LogManager.Configuration = GetDefaultConfig(); |
| 1 | 148 | | } |
| | 149 | |
|
| | 150 | | // Set the logger |
| 43 | 151 | | _logger = null != loggerType ? |
| 43 | 152 | | NLog.LogManager.GetLogger( loggerType.FullName ) : |
| 43 | 153 | | NLog.LogManager.GetCurrentClassLogger(); |
| 43 | 154 | | } |
| | 155 | |
|
| | 156 | | /// <summary>Called once the object has been disposed.</summary> |
| | 157 | | ~ImplNLog() |
| 54 | 158 | | { |
| 27 | 159 | | NLog.LogManager.Shutdown(); // Flush and close down internal threads and timers |
| 54 | 160 | | } |
| | 161 | |
|
| | 162 | | #endregion |
| | 163 | |
|
| | 164 | | #region Private Methods |
| | 165 | |
|
| | 166 | | /// <summary>Gets a default console configuration.</summary> |
| | 167 | | private static LoggingConfiguration GetDefaultConfig() |
| 1 | 168 | | { |
| | 169 | | // Create configuration object |
| 1 | 170 | | LoggingConfiguration retValue = new LoggingConfiguration(); |
| | 171 | |
|
| | 172 | | // Create target and add it to the configuration |
| 1 | 173 | | NLog.Targets.ConsoleTarget consoleTarget = new NLog.Targets.ConsoleTarget(); |
| 1 | 174 | | retValue.AddTarget( "console", consoleTarget ); |
| | 175 | |
|
| | 176 | | // Set target properties |
| 1 | 177 | | consoleTarget.Layout = cDefaultPattern; |
| | 178 | |
|
| | 179 | | // Define rules |
| 1 | 180 | | LoggingRule rule = new LoggingRule( "*", NLog.LogLevel.Debug, consoleTarget ); |
| 1 | 181 | | retValue.LoggingRules.Add( rule ); |
| | 182 | |
|
| 1 | 183 | | return retValue; |
| 1 | 184 | | } |
| | 185 | |
|
| | 186 | | #endregion |
| | 187 | | } |
| | 188 | | } |