using System; using System.Xml; namespace Wayne.Lib.Log { /// /// Log config textfile output. /// public class LogConfigTextFileOutput : LogConfigOutput { private string filePath; private LogConfigTextFileMaxSize maxSize; /// /// Output file path /// public string FilePath { get { return filePath; } set { filePath = value; XmlDocument xmlDocument = new XmlDocument(); string xml = string.Concat("", filePath, ""); xmlDocument.LoadXml(xml); LogConfigTextFilePath = new LogConfigTextFilePath(xmlDocument.DocumentElement); } } /// /// Set to true to not add 3-line file closing/appending messages /// Default is false, which does do the 3-line message /// public bool ShortFileMessages { get; set; } /// /// Max size of the file /// public LogConfigTextFileMaxSize MaxSize { get { return maxSize; } set { if (value == null) maxSize = new LogConfigTextFileMaxSize((long?)null); else maxSize = value; } } /// /// Datetime format within the file /// public string DateTimeFormat { get; set; } /// /// Entity log kind /// public EntityLogKind EntityLogKind { get; set; } /// /// Should the category be suppressed within the log file /// public bool SuppressCategory { get; set; } /// /// Keep the file opened after writing the log entry /// public bool KeepFileOpened { get; set; } internal LogConfigTextFilePath LogConfigTextFilePath { get; private set; } internal const string XmlType = "TextFileLogWriter"; /// /// Constructor /// /// The path (incl xml nodes for dynamic data). /// The size limitations wanted. Null = no limits. /// The datetime format for each line. Null = default. /// The kind of entity line prefix to use. /// Should the log category be suppressed from the log? public LogConfigTextFileOutput(string filePath, LogConfigTextFileMaxSize maxSize, string dateTimeFormat, EntityLogKind entityLogKind, bool suppressCategory) : this(filePath, maxSize, dateTimeFormat, entityLogKind, suppressCategory, true) { } /// /// Constructor /// /// The path (incl xml nodes for dynamic data). /// The size limitations wanted. Null = no limits. /// The datetime format for each line. Null = default. /// The kind of entity line prefix to use. /// Should the log category be suppressed from the log? /// Should the file be kept open or closed after each write. public LogConfigTextFileOutput(string filePath, LogConfigTextFileMaxSize maxSize, string dateTimeFormat, EntityLogKind entityLogKind, bool suppressCategory, bool keepFileOpened) { FilePath = filePath; MaxSize = maxSize; if (dateTimeFormat == null) DateTimeFormat = Logger.DefaultDateTimeFormat; else DateTimeFormat = dateTimeFormat; EntityLogKind = entityLogKind; SuppressCategory = suppressCategory; KeepFileOpened = keepFileOpened; } /// /// Deserialization constructor /// /// /// internal LogConfigTextFileOutput(XmlNode parametersNode, string ns) { XmlNode textFileParamsNode = parametersNode["TextFileParams", ns]; filePath = textFileParamsNode["FilePath", ns].InnerText; LogConfigTextFilePath = new LogConfigTextFilePath(textFileParamsNode["FilePath", ns]); MaxSize = new LogConfigTextFileMaxSize(textFileParamsNode["MaxSize", ns]); XmlNode dateTimeLogFormatNode = textFileParamsNode["DateTimeLogFormat", ns]; if (dateTimeLogFormatNode != null) DateTimeFormat = dateTimeLogFormatNode.InnerText; else DateTimeFormat = Logger.DefaultDateTimeFormat; XmlNode entityLogNode = textFileParamsNode["EntityLog", ns]; if (entityLogNode != null) EntityLogKind = (EntityLogKind)Enum.Parse(typeof(EntityLogKind), entityLogNode.InnerText, false); else EntityLogKind = EntityLogKind.Entity; XmlNode suppressCategoryNode = textFileParamsNode["SuppressCategory", ns]; SuppressCategory = suppressCategoryNode != null && XmlConvert.ToBoolean(suppressCategoryNode.InnerText); XmlNode keepFileOpenedNode = textFileParamsNode["KeepFileOpened", ns]; KeepFileOpened = keepFileOpenedNode == null || XmlConvert.ToBoolean(keepFileOpenedNode.InnerText); XmlNode shortFileMessagesNode = textFileParamsNode["ShortFileMessages", ns]; if (shortFileMessagesNode == null) { ShortFileMessages = false; } else { ShortFileMessages = XmlConvert.ToBoolean(shortFileMessagesNode.InnerText); } } /// /// Serialization /// /// internal override void WriteXml(XmlWriter xmlWriter) { xmlWriter.WriteStartElement("Output"); xmlWriter.WriteAttributeString("Type", XmlType); xmlWriter.WriteStartElement("Parameters"); xmlWriter.WriteStartElement("TextFileParams"); xmlWriter.WriteStartElement("FilePath"); xmlWriter.WriteRaw(FilePath); xmlWriter.WriteEndElement(); // FilePath MaxSize.WriteXml(xmlWriter); xmlWriter.WriteElementString("DateTimeLogFormat", DateTimeFormat); if (EntityLogKind != EntityLogKind.Entity) xmlWriter.WriteElementString("EntityLog", EntityLogKind.ToString()); if (SuppressCategory) xmlWriter.WriteElementString("SuppressCategory", XmlConvert.ToString(SuppressCategory)); if (!KeepFileOpened) xmlWriter.WriteElementString("KeepFileOpened", XmlConvert.ToString(KeepFileOpened)); xmlWriter.WriteEndElement(); // TextFileParams xmlWriter.WriteEndElement(); // Parameters xmlWriter.WriteEndElement(); // Output } /// /// Creates a clone of the object. /// /// public override object Clone() { return new LogConfigTextFileOutput(FilePath, MaxSize, DateTimeFormat, EntityLogKind, SuppressCategory, KeepFileOpened); } } }