LogEntry.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System;
  2. using System.Text;
  3. namespace Wayne.Lib.Log
  4. {
  5. /// <summary>
  6. /// An generic entry to be logged containing details regarding what to log,
  7. /// the datetime and who was performing the logging etc.
  8. ///
  9. /// This class is inherited by DebugLogEntry, EventLogEntry and ErrorLogEntry
  10. /// which adds on more specific properties.
  11. /// </summary>
  12. public class LogEntry
  13. {
  14. #region Fields
  15. private readonly EntityCategory entityCategory;
  16. private readonly DateTime dateTime;
  17. private readonly UInt64 logEntryIndex;
  18. private readonly object logObject;
  19. private static DateTime startDateTime = DateTime.Now;
  20. private static readonly int StartTick = Environment.TickCount;
  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. #if WindowsCE
  41. //since windows CE do not support milliseconds.
  42. //this is added so we can get a better understanding of performance
  43. //not exact and should probably be removed before production release
  44. dateTime = startDateTime.AddMilliseconds(Environment.TickCount - StartTick);
  45. #else
  46. dateTime = DateTime.Now;
  47. #endif
  48. //if (Logger.GlobalLogEntryCounter != null)
  49. // logEntryIndex = Logger.GlobalLogEntryCounter.GetNextValue();
  50. this.logObject = logObject;
  51. //if (Logger.IsClosed || Logger.DebugConfig == null)
  52. //{
  53. // entityCategory = new EntityCategory(entity, category);
  54. //}
  55. //else
  56. //{
  57. // entityCategory = Logger.DebugConfig.GetEntityCategory(entity, category);
  58. //}
  59. }
  60. #endregion
  61. #region Properties
  62. /// <summary>
  63. /// The EntityCategory that performed the logging.
  64. /// </summary>
  65. public EntityCategory EntityCategory
  66. {
  67. get { return entityCategory; }
  68. }
  69. /// <summary>
  70. /// The object to log.
  71. /// </summary>
  72. public object LogObject
  73. {
  74. get { return logObject; }
  75. }
  76. /// <summary>
  77. /// The date time of the logging.
  78. /// </summary>
  79. public DateTime DateTime
  80. {
  81. get { return dateTime; }
  82. }
  83. #endregion
  84. #region Methods: AppendTextToStringBuilder
  85. /// <summary>
  86. /// Appends the object to log to a StringBuilder-output.
  87. /// </summary>
  88. /// <param name="logWriter">The logwriter to be used for logging.</param>
  89. /// <param name="output">The StringBuilder.</param>
  90. /// <param name="indentLength">The indent to be used if many lines.</param>
  91. /// <param name="isFirstLine">Is this the first line to log?</param>
  92. /// <param name="indent">A string holding a generated indent-text (=a number of spaces). Use AppendIndent() to append the indent.</param>
  93. internal virtual void AppendToStringBuilder(LogWriter logWriter, StringBuilder output,
  94. int indentLength, ref bool isFirstLine, ref string indent)
  95. {
  96. StringLogObject.AppendObjectToStringBuilder(logObject, output, logWriter, indentLength, ref isFirstLine, ref indent);
  97. }
  98. /// <summary>
  99. /// Returns the datetime as a string, using the given date time format.
  100. /// </summary>
  101. /// <param name="dateTimeFormat">The requested date time format.</param>
  102. /// <returns></returns>
  103. public string GetDateTimeString(string dateTimeFormat)
  104. {
  105. //return Logger.DateTimeToString(dateTime, dateTimeFormat, logEntryIndex);
  106. return "";
  107. }
  108. #endregion
  109. #region Debug methods
  110. /// <summary>
  111. /// Presents the class as a string.
  112. /// </summary>
  113. /// <returns></returns>
  114. public virtual string ToString(string format, IFormatProvider provider)
  115. {
  116. return string.Concat("LogEntry EntityCategory=", entityCategory, ", LogObject=", (logObject != null) ? logObject.ToString() : "null");
  117. }
  118. /// <summary>
  119. /// Presents the class as a string using the specified culture-specific format information.
  120. /// </summary>
  121. /// <returns></returns>
  122. public virtual string ToString(IFormatProvider provider)
  123. {
  124. return ToString(string.Empty, provider);
  125. }
  126. /// <summary>
  127. /// Presents the class as a string using a format string.
  128. /// </summary>
  129. /// <returns></returns>
  130. public virtual string ToString(string format)
  131. {
  132. return ToString(format, System.Globalization.CultureInfo.InvariantCulture);
  133. }
  134. /// <summary>
  135. /// Presents the class as a string using a format string and the specified culture-specific format information.
  136. /// </summary>
  137. /// <returns></returns>
  138. public override string ToString()
  139. {
  140. return ToString(string.Empty, System.Globalization.CultureInfo.InvariantCulture);
  141. }
  142. #endregion
  143. }
  144. }