123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Text;
- using System.Xml;
- namespace Wayne.Lib.Log
- {
- /// <summary>
- /// This class manages the path and file name to a log file,
- /// composing the correct name depending on datetime, id-entity properties etc.
- /// </summary>
- internal class LogConfigTextFilePath
- {
- #region Inner classes
- #region Param
- abstract class Param
- {
- public abstract string ToString(EntityCategory entityCategory);
- }
- #endregion
- #region ConstantTextParam
- class ConstantTextParam : Param
- {
- private string text;
- public ConstantTextParam(string text)
- {
- this.text = text;
- }
- // MLA COMMENTED OUT THIS UNUSED CODE.
- //public string Text
- //{
- // get { return text; }
- //}
- public override string ToString(EntityCategory entityCategory)
- {
- return text;
- }
- public override string ToString()
- {
- return text;
- }
- }
- class PIDParam : Param
- {
- private string text;
- public PIDParam()
- {
- text = Process.GetCurrentProcess().Id.ToString();
- }
- public override string ToString(EntityCategory entityCategory)
- {
- return text;
- }
- public override string ToString()
- {
- return text;
- }
- }
- #endregion
- #region EntityParamType enum
- enum EntityParamType
- {
- EntityType,
- EntitySubType
- }
- #endregion
- #region EntityParam
- class EntityParam : Param
- {
- private EntityParamType paramType;
- public EntityParam(EntityParamType entityParamType)
- {
- this.paramType = entityParamType;
- }
- public override string ToString(EntityCategory entityCategory)
- {
- switch (paramType)
- {
- case EntityParamType.EntityType:
- return entityCategory.Entity.EntityType;
- case EntityParamType.EntitySubType:
- return entityCategory.Entity.EntitySubType;
- default:
- return "";
- }
- }
- public override string ToString()
- {
- return "<" + paramType.ToString() + "/>";
- }
- }
- #endregion
- #region DateParam
- class DateParam : Param
- {
- private string dateTimeFormat;
- public DateParam(string dateTimeFormat)
- {
- this.dateTimeFormat = dateTimeFormat;
- }
- public override string ToString(EntityCategory entityCategory)
- {
- return DateTime.Now.ToString(dateTimeFormat, System.Globalization.CultureInfo.InvariantCulture);
- }
- public override string ToString()
- {
- return "<Date format=" + dateTimeFormat + "/>";
- }
- }
- #endregion
- #region IdParam
- class IdParam : Param
- {
- private string fromEntityType;
- private string fromEntitySubType;
- public IdParam(string fromEntityType, string fromEntitySubType)
- {
- this.fromEntityType = fromEntityType;
- this.fromEntitySubType = fromEntitySubType;
- }
- public override string ToString(EntityCategory entityCategory)
- {
- if ((fromEntityType != null) || (fromEntitySubType != null))
- {
- IIdentifiableEntity[] ancestors = IdentifiableEntity.GetAncestorArray(entityCategory.Entity);
- if ((fromEntityType != null) && (fromEntitySubType != null))
- {
- foreach (IIdentifiableEntity ancestor in ancestors)
- {
- if (System.Text.RegularExpressions.Regex.IsMatch(ancestor.EntityType, fromEntityType, System.Text.RegularExpressions.RegexOptions.IgnoreCase) &&
- System.Text.RegularExpressions.Regex.IsMatch(ancestor.EntitySubType, fromEntitySubType, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
- {
- if (ancestor.Id != IdentifiableEntity.NoId)
- return ancestor.Id.ToString(System.Globalization.CultureInfo.InvariantCulture);
- return string.Empty;
- }
- }
- }
- else if (fromEntityType != null)
- {
- foreach (IIdentifiableEntity ancestor in ancestors)
- {
- if (System.Text.RegularExpressions.Regex.IsMatch(ancestor.EntityType, fromEntityType, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
- {
- if (ancestor.Id != IdentifiableEntity.NoId)
- return ancestor.Id.ToString(System.Globalization.CultureInfo.InvariantCulture);
- return string.Empty;
- }
- }
- }
- else
- {
- foreach (IIdentifiableEntity ancestor in ancestors)
- {
- if (System.Text.RegularExpressions.Regex.IsMatch(ancestor.EntitySubType, fromEntitySubType, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
- {
- if (ancestor.Id != IdentifiableEntity.NoId)
- return ancestor.Id.ToString(System.Globalization.CultureInfo.InvariantCulture);
- return string.Empty;
- }
- }
- }
- }
- else if (entityCategory.Entity.Id != IdentifiableEntity.NoId)
- return entityCategory.Entity.Id.ToString(System.Globalization.CultureInfo.InvariantCulture);
- return string.Empty;
- }
- public override string ToString()
- {
- return string.Format("<Id entityType={0}/>", fromEntityType);
- }
- }
- #endregion
- #region FileNameInvalidTimeResolution enum
- /// <summary>
- /// The resolution of the time when the log file gets a new name.
- /// </summary>
- enum FileNameInvalidTimeResolution
- {
- // The log file gets a new name every...
- Second,
- Minute,
- Hour,
- Day,
- Month,
- Year,
- Never
- }
- #endregion
- #endregion
- #region Fields
- private List<Param> paramList = new List<Param>();
- private FileNameInvalidTimeResolution fileNameInvalidTimeResolution; // The log file gets a new name every...
- private DateTime fileNameInvalidTime = DateTime.MinValue; // At this time the log file should get a new name.
- private Dictionary<EntityCategory, string> cachedFileNames = new Dictionary<EntityCategory, string>();
- private object cachedFileNamesLock = new object();
- private string cachedToString;
- private string cachedBaseName;
- #endregion
- #region Construction
- /// <summary>
- /// Construct a LogPath object from an configuration XML element.
- /// </summary>
- /// <param name="logPathElement"></param>
- internal LogConfigTextFilePath(XmlElement logPathElement)
- {
- fileNameInvalidTimeResolution = FileNameInvalidTimeResolution.Never;
- for (int i = 0; i < logPathElement.ChildNodes.Count; i++)
- {
- XmlNode node = logPathElement.ChildNodes[i];
- if (node.NodeType == XmlNodeType.Text)
- {
- string text = node.Value.Replace("\r", "").Replace("\n", "");
- if (i == 0)
- text = text.TrimStart(' ');
- if (i == logPathElement.ChildNodes.Count - 1)
- text = text.TrimEnd(' ');
- paramList.Add(new ConstantTextParam(text));
- }
- else if (node.NodeType == XmlNodeType.Element)
- {
- switch (node.Name)
- {
- case "Id":
- XmlAttribute fromEntityTypeAttribute = node.Attributes["FromEntityType"];
- string fromEntityType;
- if (fromEntityTypeAttribute != null)
- fromEntityType = fromEntityTypeAttribute.Value;
- else
- fromEntityType = null;
- XmlAttribute fromEntitySubTypeAttribute = node.Attributes["FromEntitySubType"];
- string fromEntitySubType;
- if (fromEntitySubTypeAttribute != null)
- fromEntitySubType = fromEntitySubTypeAttribute.Value;
- else
- fromEntitySubType = null;
- paramList.Add(new IdParam(fromEntityType, fromEntitySubType));
- break;
- case "EntityType":
- paramList.Add(new EntityParam(EntityParamType.EntityType));
- break;
- case "EntitySubType":
- paramList.Add(new EntityParam(EntityParamType.EntitySubType));
- break;
- case "PID":
- paramList.Add(new PIDParam());
- break;
- case "Date":
- string dateFormat;
- XmlAttribute formatAttribute = node.Attributes["Format"];
- if (formatAttribute != null)
- dateFormat = formatAttribute.Value;
- else
- dateFormat = "yyyyMMdd";
- paramList.Add(new DateParam(dateFormat));
- // Find the smallest time resolution in the date format. This often the file name will be refreshed.
- if ((dateFormat.IndexOf("s", StringComparison.Ordinal) > -1) || (dateFormat.IndexOf("f", StringComparison.OrdinalIgnoreCase) > -1))
- fileNameInvalidTimeResolution = FileNameInvalidTimeResolution.Second;
- else if ((dateFormat.IndexOf("m", StringComparison.Ordinal) > -1) && (fileNameInvalidTimeResolution > FileNameInvalidTimeResolution.Minute))
- fileNameInvalidTimeResolution = FileNameInvalidTimeResolution.Minute;
- else if ((dateFormat.IndexOf("h", StringComparison.OrdinalIgnoreCase) > -1) && (fileNameInvalidTimeResolution > FileNameInvalidTimeResolution.Hour))
- fileNameInvalidTimeResolution = FileNameInvalidTimeResolution.Hour;
- else if ((dateFormat.IndexOf("d", StringComparison.Ordinal) > -1) && (fileNameInvalidTimeResolution > FileNameInvalidTimeResolution.Day))
- fileNameInvalidTimeResolution = FileNameInvalidTimeResolution.Day;
- else if ((dateFormat.IndexOf("M", StringComparison.Ordinal) > -1) && (fileNameInvalidTimeResolution > FileNameInvalidTimeResolution.Month))
- fileNameInvalidTimeResolution = FileNameInvalidTimeResolution.Month;
- else if ((dateFormat.IndexOf("y", StringComparison.Ordinal) > -1) && (fileNameInvalidTimeResolution > FileNameInvalidTimeResolution.Year))
- fileNameInvalidTimeResolution = FileNameInvalidTimeResolution.Year;
- break;
- }
- }
- }
- }
- #endregion
- #region Methods
- /// <summary>
- /// Checks whether it's time for a new file name.
- /// If it is, the datetime for the next name change is calculated and all cached file names
- /// are deleted.
- /// </summary>
- public bool IsTimeForNewFileName()
- {
- if (DateTime.Now > fileNameInvalidTime)
- {
- lock (cachedFileNamesLock)
- {
- // All the cached file names are now invalid.
- cachedFileNames.Clear();
- }
- // Calculate the next time for file name change.
- DateTime now = DateTime.Now;
- switch (fileNameInvalidTimeResolution)
- {
- case FileNameInvalidTimeResolution.Second:
- fileNameInvalidTime = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second, DateTimeKind.Local).AddSeconds(1);
- break;
- case FileNameInvalidTimeResolution.Minute:
- fileNameInvalidTime = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, 0, DateTimeKind.Local).AddMinutes(1);
- break;
- case FileNameInvalidTimeResolution.Hour:
- fileNameInvalidTime = new DateTime(now.Year, now.Month, now.Day, now.Hour, 0, 0, DateTimeKind.Local).AddHours(1);
- break;
- case FileNameInvalidTimeResolution.Day:
- fileNameInvalidTime = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0, DateTimeKind.Local).AddDays(1);
- break;
- case FileNameInvalidTimeResolution.Month:
- fileNameInvalidTime = new DateTime(now.Year, now.Month, 1, 0, 0, 0, DateTimeKind.Local).AddMonths(1);
- break;
- case FileNameInvalidTimeResolution.Year:
- fileNameInvalidTime = new DateTime(now.Year, 1, 1, 0, 0, 0, DateTimeKind.Local).AddYears(1);
- break;
- case FileNameInvalidTimeResolution.Never:
- fileNameInvalidTime = DateTime.MaxValue;
- break;
- }
- return true;
- }
- return false;
- }
- /// <summary>
- /// Equality operator
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public override bool Equals(object obj)
- {
- return obj.ToString().Equals(this.ToString());
- }
- /// <summary>
- /// Hash code generator. Not correctly implemented I think....
- /// </summary>
- /// <returns></returns>
- public override int GetHashCode()
- {
- return this.ToString().GetHashCode();
- }
- /// <summary>
- /// Returns the log path using the specified identifiable entity.
- /// </summary>
- /// <returns></returns>
- public string ToString(EntityCategory entityCategory)
- {
- lock (cachedFileNamesLock)
- {
- string text;
- if (cachedFileNames.TryGetValue(entityCategory, out text))
- return text;
- StringBuilder sb = new StringBuilder();
- foreach (Param param in paramList)
- sb.Append(param.ToString(entityCategory));
- string fileName = sb.ToString();
- cachedFileNames.Add(entityCategory, fileName);
- return fileName;
- }
- }
- /// <summary>
- /// ToString method.
- /// </summary>
- /// <returns></returns>
- public override string ToString()
- {
- if (cachedToString == null)
- {
- StringBuilder sb = new StringBuilder();
- foreach (Param param in paramList)
- sb.Append(param.ToString());
- cachedToString = sb.ToString();
- }
- return cachedToString;
- }
- /// <summary>
- /// Clean the internal lists.
- /// </summary>
- /// <param name="oldestAllowedTouch"></param>
- internal void PerformListCleaning(DateTime oldestAllowedTouch)
- {
- lock (cachedFileNamesLock)
- {
- List<EntityCategory> entityCategoriesToRemove = new List<EntityCategory>();
- foreach (EntityCategory entityCategory in cachedFileNames.Keys)
- {
- if (entityCategory.LastTouched < oldestAllowedTouch)
- entityCategoriesToRemove.Add(entityCategory);
- }
- //Remove from the list.
- foreach (EntityCategory entityCategoryToRemove in entityCategoriesToRemove)
- {
- cachedFileNames.Remove(entityCategoryToRemove);
- }
- }
- }
- #endregion
- #region Properties
- /// <summary>
- /// Returns constant prefix base name - adds parameters until it finds a PID or Date parameter
- /// </summary>
- /// <returns></returns>
- public string BaseName
- {
- get
- {
- if (cachedBaseName == null)
- {
- StringBuilder sb = new StringBuilder();
- PIDParam pp = null;
- DateParam dp = null;
- IdParam ip = null;
- foreach (Param param in paramList)
- {
- pp = param as PIDParam;
- if (pp != null)
- {
- break;
- }
- dp = param as DateParam;
- if (dp != null)
- {
- break;
- }
- ip = param as IdParam;
- if (ip != null)
- {
- break;
- }
- if (param.ToString().IndexOf('<') >= 0) break;
- if (param.ToString().IndexOf('/') >= 0) break;
- sb.Append(param.ToString());
- }
- cachedBaseName = sb.ToString();
- }
- return cachedBaseName;
- }
- }
- #endregion // Properties
- }
- }
|