LogEntry.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using System;
  2. using System.Text;
  3. using System.Xml;
  4. using System.Diagnostics.CodeAnalysis;
  5. namespace Wayne.Lib.Log
  6. {
  7. /// <summary>
  8. /// An generic entry to be logged containing details regarding what to log,
  9. /// the datetime and who was performing the logging etc.
  10. ///
  11. /// This class is inherited by DebugLogEntry, EventLogEntry and ErrorLogEntry
  12. /// which adds on more specific properties.
  13. /// </summary>
  14. public class LogEntry
  15. {
  16. #region Fields
  17. private readonly EntityCategory entityCategory;
  18. private readonly DateTime dateTime;
  19. private readonly UInt64 logEntryIndex;
  20. private readonly object logObject;
  21. #endregion
  22. #region Constructors
  23. /// <summary>
  24. /// Constructor.
  25. /// </summary>
  26. /// <param name="entity">The entity that performed the logging.</param>
  27. /// <param name="logObject">The object to log.</param>
  28. internal LogEntry(IIdentifiableEntity entity, object logObject)
  29. : this(entity, logObject, string.Empty)
  30. {
  31. }
  32. /// <summary>
  33. /// Constructor.
  34. /// </summary>
  35. /// <param name="entity">The entity that performed the logging.</param>
  36. /// <param name="logObject">The object to log.</param>
  37. /// <param name="category">The category of the log object.</param>
  38. internal LogEntry(IIdentifiableEntity entity, object logObject, object category)
  39. {
  40. dateTime = DateTime.Now;
  41. if (Logger.GlobalLogEntryCounter != null)
  42. logEntryIndex = Logger.GlobalLogEntryCounter.GetNextValue();
  43. this.logObject = logObject;
  44. if (Logger.IsClosed || Logger.DebugConfig == null)
  45. {
  46. entityCategory = new EntityCategory(entity, category);
  47. }
  48. else
  49. {
  50. entityCategory = Logger.DebugConfig.GetEntityCategory(entity, category);
  51. }
  52. }
  53. /// <summary>
  54. /// Deserialization constructor.
  55. /// </summary>
  56. /// <param name="logEntryNode"></param>
  57. [SuppressMessage("Microsoft.Design", "CA1059:MembersShouldNotExposeCertainConcreteTypes", MessageId = "System.Xml.XmlNode")]
  58. internal protected LogEntry(XmlElement logEntryNode)
  59. {
  60. //Deserialize the dateTime
  61. dateTime = XmlConvert.ToDateTime(logEntryNode.Attributes["DateTime"].Value, XmlDateTimeSerializationMode.Unspecified);
  62. logEntryIndex = XmlConvert.ToUInt64(logEntryNode.Attributes["LogEntryIndex"].Value);
  63. //Create the entity category
  64. string category = logEntryNode.Attributes["Category"].Value;
  65. IIdentifiableEntity entity = null;
  66. if (logEntryNode["Entity", EventLogXml.Ns] != null)
  67. {
  68. entity = new InternalEntity(logEntryNode["Entity", EventLogXml.Ns]);
  69. }
  70. entityCategory = Logger.DebugConfig.GetEntityCategory(entity, category);
  71. //Create the log object as a sting...
  72. logObject = logEntryNode["LogObjectString", EventLogXml.Ns].InnerText;
  73. }
  74. #endregion
  75. #region Properties
  76. /// <summary>
  77. /// The EntityCategory that performed the logging.
  78. /// </summary>
  79. public EntityCategory EntityCategory
  80. {
  81. get { return entityCategory; }
  82. }
  83. /// <summary>
  84. /// The object to log.
  85. /// </summary>
  86. public object LogObject
  87. {
  88. get { return logObject; }
  89. }
  90. /// <summary>
  91. /// The date time of the logging.
  92. /// </summary>
  93. public DateTime DateTime
  94. {
  95. get { return dateTime; }
  96. }
  97. #endregion
  98. #region Methods: AppendTextToStringBuilder
  99. /// <summary>
  100. /// Appends the object to log to a StringBuilder-output.
  101. /// </summary>
  102. /// <param name="logWriter">The logwriter to be used for logging.</param>
  103. /// <param name="output">The StringBuilder.</param>
  104. /// <param name="indentLength">The indent to be used if many lines.</param>
  105. /// <param name="isFirstLine">Is this the first line to log?</param>
  106. /// <param name="indent">A string holding a generated indent-text (=a number of spaces). Use AppendIndent() to append the indent.</param>
  107. internal virtual void AppendToStringBuilder(LogWriter logWriter, StringBuilder output,
  108. int indentLength, ref bool isFirstLine, ref string indent)
  109. {
  110. StringLogObject.AppendObjectToStringBuilder(logObject, output, logWriter, indentLength, ref isFirstLine, ref indent);
  111. }
  112. /// <summary>
  113. /// Returns the datetime as a string, using the given date time format.
  114. /// </summary>
  115. /// <param name="dateTimeFormat">The requested date time format.</param>
  116. /// <returns></returns>
  117. public string GetDateTimeString(string dateTimeFormat)
  118. {
  119. return Logger.DateTimeToString(dateTime, dateTimeFormat, logEntryIndex);
  120. }
  121. #endregion
  122. #region Debug methods
  123. /// <summary>
  124. /// Presents the class as a string.
  125. /// </summary>
  126. /// <returns></returns>
  127. public virtual string ToString(string format, IFormatProvider provider)
  128. {
  129. return string.Concat("LogEntry EntityCategory=", entityCategory, ", LogObject=", (logObject != null) ? logObject.ToString() : "null");
  130. }
  131. /// <summary>
  132. /// Presents the class as a string using the specified culture-specific format information.
  133. /// </summary>
  134. /// <returns></returns>
  135. public virtual string ToString(IFormatProvider provider)
  136. {
  137. return ToString(string.Empty, provider);
  138. }
  139. /// <summary>
  140. /// Presents the class as a string using a format string.
  141. /// </summary>
  142. /// <returns></returns>
  143. public virtual string ToString(string format)
  144. {
  145. return ToString(format, System.Globalization.CultureInfo.InvariantCulture);
  146. }
  147. /// <summary>
  148. /// Presents the class as a string using a format string and the specified culture-specific format information.
  149. /// </summary>
  150. /// <returns></returns>
  151. public override string ToString()
  152. {
  153. return ToString(string.Empty, System.Globalization.CultureInfo.InvariantCulture);
  154. }
  155. #endregion
  156. #region XML
  157. /// <summary>
  158. /// Serializes this object into the specified xmlWriter.
  159. /// </summary>
  160. /// <param name="xmlWriter"></param>
  161. /// <param name="prefix"></param>
  162. public void WriteXml(XmlWriter xmlWriter, string prefix)
  163. {
  164. xmlWriter.WriteStartElement(prefix, "LogEntry", EventLogXml.Ns);
  165. xmlWriter.WriteAttributeString("Category", this.entityCategory.CategoryString);
  166. xmlWriter.WriteAttributeString("DateTime", XmlConvert.ToString(dateTime, XmlDateTimeSerializationMode.Unspecified));
  167. xmlWriter.WriteAttributeString("LogEntryIndex", XmlConvert.ToString(logEntryIndex));
  168. WriteInternalEntity(xmlWriter, prefix, this.entityCategory.Entity);
  169. string logObjectString;
  170. if (logObject != null)
  171. logObjectString = logObject.ToString();
  172. else
  173. logObjectString = string.Empty;
  174. xmlWriter.WriteElementString(prefix, "LogObjectString", EventLogXml.Ns, logObjectString);
  175. xmlWriter.WriteStartElement(prefix, "LogData", EventLogXml.Ns);
  176. WriteLogObjectData(xmlWriter);
  177. xmlWriter.WriteEndElement(); //LogData
  178. xmlWriter.WriteEndElement(); //EventLogEntry
  179. }
  180. /// <summary>
  181. ///
  182. /// </summary>
  183. /// <param name="xmlWriter"></param>
  184. protected virtual void WriteLogObjectData(XmlWriter xmlWriter)
  185. {
  186. }
  187. private static void WriteInternalEntity(XmlWriter xmlWriter, string prefix, IIdentifiableEntity entity)
  188. {
  189. if (entity == null)
  190. return;
  191. xmlWriter.WriteStartElement(prefix, "Entity", EventLogXml.Ns);
  192. xmlWriter.WriteAttributeString("EntityType", entity.EntityType);
  193. xmlWriter.WriteAttributeString("EntitySubType", entity.EntitySubType);
  194. xmlWriter.WriteAttributeString("Id", XmlConvert.ToString(entity.Id));
  195. xmlWriter.WriteStartElement(prefix, "Parent", EventLogXml.Ns);
  196. if (entity.ParentEntity != null)
  197. WriteInternalEntity(xmlWriter, prefix, entity.ParentEntity);//Write the parent entities recursively.
  198. xmlWriter.WriteEndElement(); //Parent
  199. xmlWriter.WriteEndElement(); //Entity
  200. }
  201. #endregion
  202. }
  203. }