Beispiel #1
0
        /// <summary>
        /// Reads the next complete log item from the file.
        /// </summary>
        /// <returns>The next log item in the file.</returns>
        /// <remarks>
        /// This method will block until a complete log item is available in the file.
        /// </remarks>
        public FieldLogItem ReadLogItem()
        {
            byte[]           bytes;
            FieldLogItemType type;
            int pos = -1, length;

            try
            {
                do
                {
                    // Remember the log item start position in the file
                    pos = (int)fileStream.Position;
                    bool localWaitMode = WaitMode;
                    if (!localWaitMode && pos == fileStream.Length)
                    {
                        return(null);
                    }
                    // Read the item type and length
                    bytes = ReadBytes(4);
                    if (bytes == null)
                    {
                        if (closeEvent.WaitOne(0))
                        {
                            // Close was requested
                            isClosing = true;
                            WaitMode  = false;
                            return(null);
                        }
                        if (!WaitMode && localWaitMode)
                        {
                            // Follow-up file has been noticed
                            return(null);
                        }
                        if (!localWaitMode)
                        {
                            throw new EndOfStreamException("Unexpected end of file.");
                        }
                    }
                    // Parse type and length data
                    type     = (FieldLogItemType)((bytes[0] & 0xF0) >> 4);
                    bytes[0] = (byte)(bytes[0] & 0x0F);
                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(bytes);
                    }
                    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 = ReadBytes(length);
                        // Parse the text data
                        string str = Encoding.UTF8.GetString(stringBytes);
                        // Add text to the cache
                        textCache[pos] = str;
                    }
                }while (type == FieldLogItemType.StringData);

                itemCount++;
                return(FieldLogItem.Read(this, type));
            }
            catch (Exception ex)
            {
                throw new Exception(
                          "Error reading from the log file \"" + FileName + "\" at position " + fileStream.Position + " (starting at " + pos + "). " + ex.Message,
                          ex);
            }
        }