19 using System.Collections.Generic;
25 using NLog.Targets.Wrappers;
40 public static Func<LogFile> LogFileProvider {
get;
set; }
42 public static LogFile Default {
get {
return _lazy.Value; } }
44 private static readonly Lazy<LogFile> _lazy =
new Lazy<LogFile>(
48 if (LogFileProvider != null)
50 logFile = LogFileProvider();
61 #region Static properties 67 public static bool IsInitializedAndEnabled
71 return _lazy.IsValueCreated && Default.IsFileLoggingEnabled;
79 public bool IsFileLoggingEnabled
83 return _fileLoggingEnabled;
87 if (value != _fileLoggingEnabled)
101 public NLog.LogLevel LogLevel
109 Logger.Info(
"LogLevel_set: {0} ==> {1}", _logLevel, value);
110 if (_fileRule != null)
113 _fileRule.EnableLoggingForLevels(LogLevel.Trace, value);
122 public virtual string LogFolder
126 if (_logFolder == null)
128 _logFolder =
System.IO.Path.GetTempPath();
137 public string CurrentLogFile
141 if (_currentLogPath == null)
143 _currentLogPath =
System.IO.Path.Combine(LogFolder, LOG_FILE_NAME);
145 return _currentLogPath;
152 public virtual string ArchivedLogsPath
156 if (_archivedLogsPath == null)
158 _archivedLogsPath =
System.IO.Path.Combine(LogFolder, ARCHIVE_FILE_NAME);
160 return _archivedLogsPath;
169 public string CurrentLog
175 return System.IO.File.ReadAllText(CurrentLogFile);
177 catch (
System.IO.IOException e)
190 public bool IsCurrentLogAvailable
194 return System.IO.File.Exists(CurrentLogFile);
204 _logLevel =
NLog.LogLevel.Info;
205 _config =
new LoggingConfiguration();
206 LogManager.Configuration = _config;
211 #region Public methods 213 public void ShowFolderInExplorer()
215 System.Diagnostics.Process.Start(
new System.Diagnostics.ProcessStartInfo(LogFolder));
218 public void EnableDebugLogging()
220 if (!_debugLoggingEnabled)
222 _debugLoggingEnabled =
true;
223 DebuggerTarget t =
new DebuggerTarget();
224 _config.AddTarget(DEBUG_TARGET, t);
225 _config.LoggingRules.Add(
new LoggingRule(
"*", LogLevel.Debug, t));
226 LogManager.ReconfigExistingLoggers();
232 #region Protected properties and methods 234 protected void EnableFileLogging()
236 _fileLoggingEnabled =
true;
237 if (_fileTarget == null)
239 _fileTarget =
new FileTarget();
240 _fileTarget.FileName = CurrentLogFile;
241 _fileTarget.ArchiveFileName = ArchivedLogsPath;
242 _fileTarget.ArchiveNumbering = ArchiveNumberingMode.Date;
243 _fileTarget.ArchiveDateFormat = ARCHIVE_DATE_FORMAT;
244 _fileTarget.ArchiveEvery = FileArchivePeriod.Day;
245 _fileTarget.MaxArchiveFiles = MAX_ARCHIVE_FILES;
246 AsyncTargetWrapper wrapper =
new AsyncTargetWrapper(_fileTarget);
247 _config.AddTarget(FILE_TARGET, wrapper);
249 if (_fileRule == null)
251 _fileRule =
new LoggingRule(
"*", LogLevel, _fileTarget);
253 _config.LoggingRules.Add(_fileRule);
254 LogManager.ReconfigExistingLoggers();
255 Logger.Info(
"===== Begin file logging =====");
258 protected void DisableFileLogging()
260 Logger.Info(
"Disabling file logging");
261 _config.LoggingRules.Remove(_fileRule);
262 LogManager.ReconfigExistingLoggers();
263 _fileLoggingEnabled =
false;
268 #region Private fields 270 private string _logFolder;
271 private string _currentLogPath;
272 private string _archivedLogsPath;
273 private bool _debugLoggingEnabled;
274 private bool _fileLoggingEnabled;
275 private NLog.LogLevel _logLevel;
276 private LoggingConfiguration _config;
277 private FileTarget _fileTarget;
278 private LoggingRule _fileRule;
282 #region Private constants 284 private const string FILE_TARGET =
"file";
285 private const string DEBUG_TARGET =
"debug";
286 private const string LOG_FILE_NAME =
"current-log.txt";
287 private const string ARCHIVE_FILE_NAME =
"log-{#}.txt";
288 private const string ARCHIVE_DATE_FORMAT =
"yyyy-MM-dd";
289 private const int MAX_ARCHIVE_FILES = 7;
295 private static NLog.Logger Logger {
get {
return _logger.Value; } }
297 private static readonly Lazy<
NLog.Logger> _logger =
new Lazy<
NLog.Logger>(() =>
NLog.LogManager.GetCurrentClassLogger());
Provides logging to file and to the debug console; wraps NLog configuration and targets.