LogConfigTextFileOutput.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. /// Should the category be suppressed within the log file
  56. /// </summary>
  57. public bool SuppressCategory { get; set; }
  58. /// <summary>
  59. /// Keep the file opened after writing the log entry
  60. /// </summary>
  61. public bool KeepFileOpened { get; set; }
  62. internal LogConfigTextFilePath LogConfigTextFilePath { get; private set; }
  63. internal const string XmlType = "TextFileLogWriter";
  64. /// <summary>
  65. /// Constructor
  66. /// </summary>
  67. /// <param name="filePath">The path (incl xml nodes for dynamic data).</param>
  68. /// <param name="maxSize">The size limitations wanted. Null = no limits.</param>
  69. /// <param name="dateTimeFormat">The datetime format for each line. Null = default.</param>
  70. /// <param name="entityLogKind">The kind of entity line prefix to use.</param>
  71. /// <param name="suppressCategory">Should the log category be suppressed from the log?</param>
  72. public LogConfigTextFileOutput(string filePath, LogConfigTextFileMaxSize maxSize, string dateTimeFormat,
  73. EntityLogKind entityLogKind, bool suppressCategory)
  74. : this(filePath, maxSize, dateTimeFormat, entityLogKind, suppressCategory, true)
  75. {
  76. }
  77. /// <summary>
  78. /// Constructor
  79. /// </summary>
  80. /// <param name="filePath">The path (incl xml nodes for dynamic data).</param>
  81. /// <param name="maxSize">The size limitations wanted. Null = no limits.</param>
  82. /// <param name="dateTimeFormat">The datetime format for each line. Null = default.</param>
  83. /// <param name="entityLogKind">The kind of entity line prefix to use.</param>
  84. /// <param name="suppressCategory">Should the log category be suppressed from the log?</param>
  85. /// <param name="keepFileOpened">Should the file be kept open or closed after each write.</param>
  86. public LogConfigTextFileOutput(string filePath, LogConfigTextFileMaxSize maxSize, string dateTimeFormat,
  87. EntityLogKind entityLogKind, bool suppressCategory, bool keepFileOpened)
  88. {
  89. FilePath = filePath;
  90. MaxSize = maxSize;
  91. if (dateTimeFormat == null)
  92. DateTimeFormat = Logger.DefaultDateTimeFormat;
  93. else
  94. DateTimeFormat = dateTimeFormat;
  95. EntityLogKind = entityLogKind;
  96. SuppressCategory = suppressCategory;
  97. KeepFileOpened = keepFileOpened;
  98. }
  99. /// <summary>
  100. /// Deserialization constructor
  101. /// </summary>
  102. /// <param name="parametersNode"></param>
  103. /// <param name="ns"></param>
  104. internal LogConfigTextFileOutput(XmlNode parametersNode, string ns)
  105. {
  106. XmlNode textFileParamsNode = parametersNode["TextFileParams", ns];
  107. filePath = textFileParamsNode["FilePath", ns].InnerText;
  108. LogConfigTextFilePath = new LogConfigTextFilePath(textFileParamsNode["FilePath", ns]);
  109. MaxSize = new LogConfigTextFileMaxSize(textFileParamsNode["MaxSize", ns]);
  110. XmlNode dateTimeLogFormatNode = textFileParamsNode["DateTimeLogFormat", ns];
  111. if (dateTimeLogFormatNode != null)
  112. DateTimeFormat = dateTimeLogFormatNode.InnerText;
  113. else
  114. DateTimeFormat = Logger.DefaultDateTimeFormat;
  115. XmlNode entityLogNode = textFileParamsNode["EntityLog", ns];
  116. if (entityLogNode != null)
  117. EntityLogKind = (EntityLogKind)Enum.Parse(typeof(EntityLogKind), entityLogNode.InnerText, false);
  118. else
  119. EntityLogKind = EntityLogKind.Entity;
  120. XmlNode suppressCategoryNode = textFileParamsNode["SuppressCategory", ns];
  121. SuppressCategory = suppressCategoryNode != null && XmlConvert.ToBoolean(suppressCategoryNode.InnerText);
  122. XmlNode keepFileOpenedNode = textFileParamsNode["KeepFileOpened", ns];
  123. KeepFileOpened = keepFileOpenedNode == null || XmlConvert.ToBoolean(keepFileOpenedNode.InnerText);
  124. XmlNode shortFileMessagesNode = textFileParamsNode["ShortFileMessages", ns];
  125. if (shortFileMessagesNode == null)
  126. {
  127. ShortFileMessages = false;
  128. }
  129. else
  130. {
  131. ShortFileMessages = XmlConvert.ToBoolean(shortFileMessagesNode.InnerText);
  132. }
  133. }
  134. /// <summary>
  135. /// Serialization
  136. /// </summary>
  137. /// <param name="xmlWriter"></param>
  138. internal override void WriteXml(XmlWriter xmlWriter)
  139. {
  140. xmlWriter.WriteStartElement("Output");
  141. xmlWriter.WriteAttributeString("Type", XmlType);
  142. xmlWriter.WriteStartElement("Parameters");
  143. xmlWriter.WriteStartElement("TextFileParams");
  144. xmlWriter.WriteStartElement("FilePath");
  145. xmlWriter.WriteRaw(FilePath);
  146. xmlWriter.WriteEndElement(); // FilePath
  147. MaxSize.WriteXml(xmlWriter);
  148. xmlWriter.WriteElementString("DateTimeLogFormat", DateTimeFormat);
  149. if (EntityLogKind != EntityLogKind.Entity)
  150. xmlWriter.WriteElementString("EntityLog", EntityLogKind.ToString());
  151. if (SuppressCategory)
  152. xmlWriter.WriteElementString("SuppressCategory", XmlConvert.ToString(SuppressCategory));
  153. if (!KeepFileOpened)
  154. xmlWriter.WriteElementString("KeepFileOpened", XmlConvert.ToString(KeepFileOpened));
  155. xmlWriter.WriteEndElement(); // TextFileParams
  156. xmlWriter.WriteEndElement(); // Parameters
  157. xmlWriter.WriteEndElement(); // Output
  158. }
  159. /// <summary>
  160. /// Creates a clone of the object.
  161. /// </summary>
  162. /// <returns></returns>
  163. public override object Clone()
  164. {
  165. return new LogConfigTextFileOutput(FilePath, MaxSize, DateTimeFormat, EntityLogKind, SuppressCategory, KeepFileOpened);
  166. }
  167. }
  168. }