LogConfigTextFileOutput.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.Xml;
  3. namespace Wayne.Lib.Log
  4. {
  5. /// <summary>
  6. /// Log config textfile output.
  7. /// </summary>
  8. public class LogConfigTextFileOutput : LogConfigOutput
  9. {
  10. private string filePath;
  11. private LogConfigTextFileMaxSize maxSize;
  12. /// <summary>
  13. /// Output file path
  14. /// </summary>
  15. public string FilePath
  16. {
  17. get { return filePath; }
  18. set
  19. {
  20. filePath = value;
  21. XmlDocument xmlDocument = new XmlDocument();
  22. string xml = string.Concat("<FilePath>", filePath, "</FilePath>");
  23. xmlDocument.LoadXml(xml);
  24. LogConfigTextFilePath = new LogConfigTextFilePath(xmlDocument.DocumentElement);
  25. }
  26. }
  27. /// <summary>
  28. /// Set to true to not add 3-line file closing/appending messages
  29. /// Default is false, which does do the 3-line message
  30. /// </summary>
  31. public bool ShortFileMessages { get; set; }
  32. /// <summary>
  33. /// Max size of the file
  34. /// </summary>
  35. public LogConfigTextFileMaxSize MaxSize
  36. {
  37. get { return maxSize; }
  38. set
  39. {
  40. if (value == null)
  41. maxSize = new LogConfigTextFileMaxSize((long?)null);
  42. else
  43. maxSize = value;
  44. }
  45. }
  46. /// <summary>
  47. /// Datetime format within the file
  48. /// </summary>
  49. public string DateTimeFormat { get; set; }
  50. /// <summary>
  51. /// Entity log kind
  52. /// </summary>
  53. public EntityLogKind EntityLogKind { get; set; }
  54. /// <summary>
  55. /// Flushes to disk for every line
  56. /// </summary>
  57. public bool ForceFlush { get; set; }
  58. /// <summary>
  59. /// Should the category be suppressed within the log file
  60. /// </summary>
  61. public bool SuppressCategory { get; set; }
  62. /// <summary>
  63. /// Keep the file opened after writing the log entry
  64. /// </summary>
  65. public bool KeepFileOpened { get; set; }
  66. internal LogConfigTextFilePath LogConfigTextFilePath { get; private set; }
  67. internal const string XmlType = "TextFileLogWriter";
  68. /// <summary>
  69. /// Constructor
  70. /// </summary>
  71. /// <param name="filePath">The path (incl xml nodes for dynamic data).</param>
  72. /// <param name="maxSize">The size limitations wanted. Null = no limits.</param>
  73. /// <param name="dateTimeFormat">The datetime format for each line. Null = default.</param>
  74. /// <param name="entityLogKind">The kind of entity line prefix to use.</param>
  75. /// <param name="suppressCategory">Should the log category be suppressed from the log?</param>
  76. public LogConfigTextFileOutput(string filePath, LogConfigTextFileMaxSize maxSize, string dateTimeFormat,
  77. EntityLogKind entityLogKind, bool suppressCategory)
  78. : this(filePath, maxSize, dateTimeFormat, entityLogKind, suppressCategory, true)
  79. {
  80. }
  81. /// <summary>
  82. /// Constructor
  83. /// </summary>
  84. /// <param name="filePath">The path (incl xml nodes for dynamic data).</param>
  85. /// <param name="maxSize">The size limitations wanted. Null = no limits.</param>
  86. /// <param name="dateTimeFormat">The datetime format for each line. Null = default.</param>
  87. /// <param name="entityLogKind">The kind of entity line prefix to use.</param>
  88. /// <param name="suppressCategory">Should the log category be suppressed from the log?</param>
  89. /// <param name="keepFileOpened">Should the file be kept open or closed after each write.</param>
  90. public LogConfigTextFileOutput(string filePath, LogConfigTextFileMaxSize maxSize, string dateTimeFormat,
  91. EntityLogKind entityLogKind, bool suppressCategory, bool keepFileOpened)
  92. {
  93. FilePath = filePath;
  94. MaxSize = maxSize;
  95. if (dateTimeFormat == null)
  96. DateTimeFormat = Logger.DefaultDateTimeFormat;
  97. else
  98. DateTimeFormat = dateTimeFormat;
  99. EntityLogKind = entityLogKind;
  100. SuppressCategory = suppressCategory;
  101. KeepFileOpened = keepFileOpened;
  102. }
  103. /// <summary>
  104. /// Deserialization constructor
  105. /// </summary>
  106. /// <param name="parametersNode"></param>
  107. /// <param name="ns"></param>
  108. internal LogConfigTextFileOutput(XmlNode parametersNode, string ns)
  109. {
  110. XmlNode textFileParamsNode = parametersNode["TextFileParams", ns];
  111. filePath = textFileParamsNode["FilePath", ns].InnerText;
  112. LogConfigTextFilePath = new LogConfigTextFilePath(textFileParamsNode["FilePath", ns]);
  113. MaxSize = new LogConfigTextFileMaxSize(textFileParamsNode["MaxSize", ns]);
  114. XmlNode dateTimeLogFormatNode = textFileParamsNode["DateTimeLogFormat", ns];
  115. if (dateTimeLogFormatNode != null)
  116. DateTimeFormat = dateTimeLogFormatNode.InnerText;
  117. else
  118. DateTimeFormat = Logger.DefaultDateTimeFormat;
  119. XmlNode entityLogNode = textFileParamsNode["EntityLog", ns];
  120. if (entityLogNode != null)
  121. EntityLogKind = (EntityLogKind)Enum.Parse(typeof(EntityLogKind), entityLogNode.InnerText, false);
  122. else
  123. EntityLogKind = EntityLogKind.Entity;
  124. XmlNode forceFlushNode = textFileParamsNode["ForceFlush", ns];
  125. ForceFlush = forceFlushNode == null || XmlConvert.ToBoolean(forceFlushNode.InnerText);
  126. XmlNode suppressCategoryNode = textFileParamsNode["SuppressCategory", ns];
  127. SuppressCategory = suppressCategoryNode != null && XmlConvert.ToBoolean(suppressCategoryNode.InnerText);
  128. XmlNode keepFileOpenedNode = textFileParamsNode["KeepFileOpened", ns];
  129. KeepFileOpened = keepFileOpenedNode == null || XmlConvert.ToBoolean(keepFileOpenedNode.InnerText);
  130. XmlNode shortFileMessagesNode = textFileParamsNode["ShortFileMessages", ns];
  131. if (shortFileMessagesNode == null)
  132. {
  133. ShortFileMessages = false;
  134. }
  135. else
  136. {
  137. ShortFileMessages = XmlConvert.ToBoolean(shortFileMessagesNode.InnerText);
  138. }
  139. }
  140. /// <summary>
  141. /// Serialization
  142. /// </summary>
  143. /// <param name="xmlWriter"></param>
  144. internal override void WriteXml(XmlWriter xmlWriter)
  145. {
  146. xmlWriter.WriteStartElement("Output");
  147. xmlWriter.WriteAttributeString("Type", XmlType);
  148. xmlWriter.WriteStartElement("Parameters");
  149. xmlWriter.WriteStartElement("TextFileParams");
  150. xmlWriter.WriteStartElement("FilePath");
  151. xmlWriter.WriteRaw(FilePath);
  152. xmlWriter.WriteEndElement(); // FilePath
  153. MaxSize.WriteXml(xmlWriter);
  154. xmlWriter.WriteElementString("DateTimeLogFormat", DateTimeFormat);
  155. if (EntityLogKind != EntityLogKind.Entity)
  156. xmlWriter.WriteElementString("EntityLog", EntityLogKind.ToString());
  157. if (SuppressCategory)
  158. xmlWriter.WriteElementString("SuppressCategory", XmlConvert.ToString(SuppressCategory));
  159. if (!KeepFileOpened)
  160. xmlWriter.WriteElementString("KeepFileOpened", XmlConvert.ToString(KeepFileOpened));
  161. xmlWriter.WriteEndElement(); // TextFileParams
  162. xmlWriter.WriteEndElement(); // Parameters
  163. xmlWriter.WriteEndElement(); // Output
  164. }
  165. /// <summary>
  166. /// Creates a clone of the object.
  167. /// </summary>
  168. /// <returns></returns>
  169. public override object Clone()
  170. {
  171. return new LogConfigTextFileOutput(FilePath, MaxSize, DateTimeFormat, EntityLogKind, SuppressCategory, KeepFileOpened);
  172. }
  173. }
  174. }