123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics.CodeAnalysis;
- namespace Wayne.Lib.Log
- {
- /// <summary>
- /// Class used to make debug logs.
- /// </summary>
- /// <example>
- /// This is an example of how to write a debug log entry.
- /// <code>
- /// using (DebugLogger dLog = new DebugLogger(this))
- /// {
- /// if (dLog.IsActive(DebugLogLevel.Detailed))
- /// {
- /// dLog.Add("This is line 1.", DebugLogLevel.Detailed);
- /// dLog.Add("This is line 2.", "MyCategory", DebugLogLevel.Detailed);
- /// }
- /// }
- /// </code>
- /// </example>
- public sealed class DebugLogger : IDisposable, IIdentifiableEntity, IDebugLogger
- {
- static NLog.Logger fdcClientLogger = NLog.LogManager.LoadConfiguration("nlog.config").GetLogger("FdcClient");
- #region Fields
- private IIdentifiableEntity entity;
- private bool persistent;
- private object categoryDebugLevelLock = new object();
- private Dictionary<object, DebugLogLevel> categoryDebugLevel = new Dictionary<object, DebugLogLevel>();
- private bool logPersistentInfo;
- private bool disposed;
- #endregion
- #region Constructors
- /// <summary>
- /// Construction of non-persistent DebugLogger.
- /// </summary>
- /// <param name="entity"></param>
- public DebugLogger(IIdentifiableEntity entity)
- {
- if (Logger.IsClosed)
- disposed = true;// throw new LogException(LogExceptionType.LoggerClosed);
- if (entity != null)
- this.entity = entity;
- else
- this.entity = IdentifiableEntity.Empty;
- }
- /// <summary>
- /// Construction
- /// </summary>
- /// <param name="entity"></param>
- /// <param name="persistent"></param>
- public DebugLogger(IIdentifiableEntity entity, bool persistent)
- {
- //if (Logger.IsClosed)
- // disposed = true;// throw new LogException(LogExceptionType.LoggerClosed);
- //else
- //{
- // if (entity != null)
- // this.entity = entity;
- // else
- // this.entity = IdentifiableEntity.Empty;
- // this.persistent = persistent;
- // if (persistent)
- // {
- // Logger.RegisterPersistentLogObject(this as DebugLogger);
- // logPersistentInfo = true;
- // }
- //}
- }
- /// <summary>
- /// Finalizer.
- /// </summary>
- ~DebugLogger()
- {
- Dispose(false);
- }
- #endregion
- #region IDisposable Members
- /// <summary>
- /// Dispose.
- /// </summary>
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- /// <summary>
- /// Dispose.
- /// </summary>
- private void Dispose(bool disposing)
- {
- if (!disposed)
- {
- disposed = true;
- if (disposing)
- {
- if (persistent)
- {
- Logger.UnregisterPersistentLogObject(this as DebugLogger);
- }
- Logger.UnregisterEntity(entity);
- }
- lock (categoryDebugLevelLock)
- {
- categoryDebugLevel.Clear();
- }
- }
- }
- #endregion
- #region Properties
- /// <summary>
- /// The identifiable entity that has created this debug log.
- /// </summary>
- public IIdentifiableEntity Entity
- {
- get { return entity; }
- }
- /// <summary>
- /// Tells whether the debug log is persistent or not.
- /// </summary>
- public bool Persistent
- {
- get { return persistent; }
- }
- #endregion
- #region Methods: IsActive
- /// <summary>
- /// Tells whether the default category is active in the Normal level.
- /// </summary>
- public bool IsActive()
- {
- return true;
- return !disposed && (DebugLogLevel.Normal <= GetDebugLevel(string.Empty));
- }
- /// <summary>
- /// Tells whether the given category is active in the Normal level.
- /// </summary>
- public bool IsActive(object category)
- {
- return true;
- return !disposed && (DebugLogLevel.Normal <= GetDebugLevel(category));
- }
- /// <summary>
- /// Tells whether the default category is active in the given level.
- /// </summary>
- public bool IsActive(DebugLogLevel debugLogLevel)
- {
- return true;
- return !disposed && (debugLogLevel != DebugLogLevel.Excluded) && (debugLogLevel <= GetDebugLevel(string.Empty));
- }
- /// <summary>
- /// Tells whether the given category is active in the given level.
- /// </summary>
- public bool IsActive(object category, DebugLogLevel debugLogLevel)
- {
- return true;
- return !disposed && (debugLogLevel != DebugLogLevel.Excluded) && (debugLogLevel <= GetDebugLevel(category));
- }
- #endregion
- #region Methods: GetDebugLevel
- /// <summary>
- /// Get the current debug level for the default category.
- /// </summary>
- public DebugLogLevel GetDebugLevel()
- {
- return GetDebugLevel(string.Empty);
- }
- /// <summary>
- /// Get the current debug level for the given category.
- /// </summary>
- public DebugLogLevel GetDebugLevel(object category)
- {
- return DebugLogLevel.Detailed;
- if (disposed || Logger.IsClosed)
- return DebugLogLevel.Excluded;
- if (category == null)
- category = string.Empty;
- // Check if this category's debuglevel is cached.
- lock (categoryDebugLevelLock)
- {
- DebugLogLevel cachedDebugLogLevel;
- if (categoryDebugLevel.TryGetValue(category, out cachedDebugLogLevel))
- return cachedDebugLogLevel;
- }
- // Get a list of the logWriters that wants to log me.
- EntityCategory entityCategory = Logger.DebugConfig.GetEntityCategory(entity, category);
- LogWriter[] logWriters = Logger.DebugConfig.GetLogWriters(entityCategory);
- // Iterate through the writers and get the highest debug level.
- DebugLogLevel debugLogLevel = DebugLogLevel.Excluded;
- foreach (LogWriter writer in logWriters)
- {
- debugLogLevel = MaxDebugLogLevel(debugLogLevel, writer.GetDebugLogLevel(entityCategory));
- }
- // Cache this debug level for future queries.
- lock (categoryDebugLevelLock)
- {
- categoryDebugLevel[category] = debugLogLevel;
- }
- return debugLogLevel;
- }
- #endregion
- #region Methods: Add
- /// <summary>
- /// Adds a new object to the debug log entry.
- /// </summary>
- /// <param name="obj">The log object that are added.</param>
- [SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "obj")]
- public void Add(object obj)
- {
- fdcClientLogger.Debug(obj);
- return;
- if (disposed)
- return;
- if (logPersistentInfo)
- LogPersistentInfo();
- Logger.AddEntry(new DebugLogEntry(entity, obj));
- }
- /// <summary>
- /// Adds the object if debuglogger is active with the default category and detaillevel.
- /// </summary>
- /// <param name="args"></param>
- /// <param name="formatString"></param>
- public void AddIfActive(string formatString, params object[] args)
- {
- fdcClientLogger.Debug(string.Format(formatString, args));
- return;
- if (IsActive())
- {
- try
- {
- Add(string.Format(formatString, args));
- }
- catch (FormatException fe)
- {
- Add(fe);
- Add(formatString);
- }
- }
- }
- /// <summary>
- /// Adds the object if debuglogger is active with the default category and detaillevel Detailed .
- /// </summary>
- /// <param name="formatString"></param>
- /// <param name="args"></param>
- public void AddIfActiveDetailed(string formatString, params object[] args)
- {
- fdcClientLogger.Debug(string.Format(formatString, args));
- return;
- if (IsActive(DebugLogLevel.Detailed))
- {
- try
- {
- Add(string.Format(formatString, args), DebugLogLevel.Detailed);
- }
- catch (FormatException fe)
- {
- Add(fe);
- Add(formatString);
- }
- }
- }
- /// <summary>
- /// Adds a new object to the debug log entry.
- /// </summary>
- /// <param name="obj">The log object that are added.</param>
- /// <param name="level">TDB</param>
- public void Add(object obj, DebugLogLevel level)
- {
- fdcClientLogger.Debug(obj);
- return;
- if (disposed)
- return;
- if (logPersistentInfo)
- LogPersistentInfo();
- Logger.AddEntry(new DebugLogEntry(entity, obj, level));
- }
- /// <summary>
- /// Adds a new object to the debug log entry.
- /// </summary>
- /// <param name="obj">The log object that are added.</param>
- /// <param name="category">A specific category that this log is about.</param>
- [SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "obj")]
- public void Add(object obj, object category)
- {
- fdcClientLogger.Debug(obj);
- return;
- if (disposed)
- return;
- if (logPersistentInfo)
- LogPersistentInfo();
- Logger.AddEntry(new DebugLogEntry(entity, obj, category));
- }
- /// <summary>
- /// Adds a new object to the debug log entry.
- /// </summary>
- /// <param name="obj">The log object that are added.</param>
- /// <param name="category">A specific category that this log is about.</param>
- /// <param name="level">TDB</param>
- [SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "obj")]
- public void Add(object obj, object category, DebugLogLevel level)
- {
- fdcClientLogger.Debug(obj);
- return;
- if (disposed)
- return;
- if (logPersistentInfo)
- LogPersistentInfo();
- Logger.AddEntry(new DebugLogEntry(entity, obj, category, level));
- }
- /// <summary>
- ///
- /// </summary>
- private void LogPersistentInfo()
- {
- //Logger.AddEntry(new DebugLogEntry(entity, "Hooked persistent DebugLogger to logfile: " + IdentifiableEntity.ToString(entity, true), string.Empty, DebugLogLevel.Normal));
- //logPersistentInfo = false;
- }
- #endregion
- #region Internal Methods
- /// <summary>
- /// Invalidates the internal flags for configuration reading.
- /// </summary>
- internal void Invalidate()
- {
- //lock (categoryDebugLevelLock)
- //{
- // categoryDebugLevel.Clear();
- //}
- }
- /// <summary>
- /// Static method to get the highest of two DebugLogLevel's.
- /// </summary>
- /// <param name="level1"></param>
- /// <param name="level2"></param>
- /// <returns></returns>
- public static DebugLogLevel MaxDebugLogLevel(DebugLogLevel level1, DebugLogLevel level2)
- {
- if (level1 > level2)
- return level1;
- else
- return level2;
- }
- #endregion
- #region IIdentifiableEntity Members
- /// <summary>
- /// The Id of the Entity.
- /// </summary>
- public int Id
- {
- get { return entity.Id; }
- }
- /// <summary>
- /// The EntityType of the Entity.
- /// </summary>
- public string EntityType
- {
- get { return entity.EntityType; }
- }
- /// <summary>
- /// This is used by the logger and should never be set by implementing classes
- /// </summary>
- public string FullEntityName { get; set; }
- /// <summary>
- /// The EntitySubType of the Entity.
- /// </summary>
- public string EntitySubType
- {
- get { return entity.EntitySubType; }
- }
- /// <summary>
- /// The ParentEntity of the Entity.
- /// </summary>
- public IIdentifiableEntity ParentEntity
- {
- get { return entity.ParentEntity; }
- }
- #endregion
- }
- }
|