private void ReadTextCache() { byte[] bytes = new byte[4]; // Read from the file till the end while (fileStream.Position < fileStream.Length) { // Remember the log item start position in the file int pos = (int)fileStream.Position; // Read the item type and length if (fileStream.Read(bytes, 0, bytes.Length) < bytes.Length) { throw new FormatException("Invalid log file to append to. Log item header too short."); } // Parse type and length data FieldLogItemType type = (FieldLogItemType)((bytes[0] & 0xF0) >> 4); bytes[0] = (byte)(bytes[0] & 0x0F); if (BitConverter.IsLittleEndian) { Array.Reverse(bytes); } int length = BitConverter.ToInt32(bytes, 0); // Check whether this is a text item if (type == FieldLogItemType.StringData) { // Read the item and add it to the text cache byte[] stringBytes = new byte[length]; if (fileStream.Read(stringBytes, 0, length) < length) { throw new FormatException("Invalid log file to append to. Text log item shorter than indicated."); } // Parse the text data string str = Encoding.UTF8.GetString(stringBytes); // Add text to the cache textCache[str] = pos; } else { // Seek over the item and verify that it worked long expected = fileStream.Position + length; if (fileStream.Seek(length, SeekOrigin.Current) != expected) { throw new FormatException("Invalid log file to append to. Log item shorter than indicated."); } } } }
/// <summary> /// Reads a log item from the log file. /// </summary> /// <param name="reader">Log file reader.</param> /// <param name="type">The type of log item to read.</param> /// <returns>The read log item.</returns> internal static FieldLogItem Read(FieldLogFileReader reader, FieldLogItemType type) { switch (type) { case FieldLogItemType.Text: return(FieldLogTextItem.Read(reader)); case FieldLogItemType.Data: return(FieldLogDataItem.Read(reader)); case FieldLogItemType.Exception: return(FieldLogExceptionItem.Read(reader)); case FieldLogItemType.Scope: case FieldLogItemType.RepeatedScope: return(FieldLogScopeItem.Read(reader, type == FieldLogItemType.RepeatedScope)); } throw new ArgumentException("Unsupported log item type (" + (int)type + ")"); }
/// <summary> /// Sets the type of the item to write. /// </summary> /// <param name="itemType">Log item type.</param> internal void SetItemType(FieldLogItemType itemType) { this.itemType = (byte)itemType; }
/// <summary> /// Sets the type of the item to write. /// </summary> /// <param name="itemType">Log item type.</param> internal void SetItemType(FieldLogItemType itemType) { this.itemType = (byte)itemType; }
/// <summary> /// Reads a log item from the log file. /// </summary> /// <param name="reader">Log file reader.</param> /// <param name="type">The type of log item to read.</param> /// <returns>The read log item.</returns> internal static FieldLogItem Read(FieldLogFileReader reader, FieldLogItemType type) { switch (type) { case FieldLogItemType.Text: return FieldLogTextItem.Read(reader); case FieldLogItemType.Data: return FieldLogDataItem.Read(reader); case FieldLogItemType.Exception: return FieldLogExceptionItem.Read(reader); case FieldLogItemType.Scope: case FieldLogItemType.RepeatedScope: return FieldLogScopeItem.Read(reader, type == FieldLogItemType.RepeatedScope); } throw new ArgumentException("Unsupported log item type (" + (int)type + ")"); }