This topic has not yet been rated - Rate this topic

FileRecordSequence Class

Implements an IRecordSequence on top of a file. This class cannot be inherited.

System.Object
  System.IO.Log.FileRecordSequence

Namespace:  System.IO.Log
Assembly:  System.IO.Log (in System.IO.Log.dll)
public sealed class FileRecordSequence : IRecordSequence, 
	IDisposable

The FileRecordSequence type exposes the following members.

  Name Description
Public method FileRecordSequence(String) Initializes a new instance of the FileRecordSequence class with a specified file.
Public method FileRecordSequence(String, FileAccess) Initializes a new instance of the FileRecordSequence class with a specified file and an access mode.
Public method FileRecordSequence(String, FileAccess, Int32) Initializes a new instance of the FileRecordSequence class with a specified file, an access mode, and a file size.
Top
  Name Description
Public property BaseSequenceNumber Gets the sequence number of the first valid record in the current FileRecordSequence.
Public property LastSequenceNumber Gets the sequence number which is greater than the last record appended.
Public property MaximumRecordLength Gets the size of the largest record that can be appended to or read from this sequence, in bytes.
Public property ReservedBytes Gets the total number of bytes that have been reserved.
Public property RestartSequenceNumber Gets the sequence number of the most recently written restart area.
Public property RetryAppend Gets or sets a value indicating whether or not appends are automatically retried if the log is full.
Top
  Name Description
Public method AdvanceBaseSequenceNumber Moves the base sequence number of the log forward. This method cannot be inherited.
Public method Append(ArraySegment<Byte>, SequenceNumber, SequenceNumber, RecordAppendOptions) Writes a log record to the FileRecordSequence. This method cannot be inherited.
Public method Append(IList<ArraySegment<Byte>>, SequenceNumber, SequenceNumber, RecordAppendOptions) Writes a log record to the FileRecordSequence. This method cannot be inherited.
Public method Append(ArraySegment<Byte>, SequenceNumber, SequenceNumber, RecordAppendOptions, ReservationCollection) Writes a log record to the FileRecordSequence, using space previously reserved in the sequence. This method cannot be inherited.
Public method Append(IList<ArraySegment<Byte>>, SequenceNumber, SequenceNumber, RecordAppendOptions, ReservationCollection) Writes a log record to the FileRecordSequence, using space previously reserved in the sequence. This method cannot be inherited.
Public method BeginAppend(ArraySegment<Byte>, SequenceNumber, SequenceNumber, RecordAppendOptions, AsyncCallback, Object) Begins an asynchronous append operation. This method cannot be inherited.
Public method BeginAppend(IList<ArraySegment<Byte>>, SequenceNumber, SequenceNumber, RecordAppendOptions, AsyncCallback, Object) Begins an asynchronous append operation. This method cannot be inherited.
Public method BeginAppend(ArraySegment<Byte>, SequenceNumber, SequenceNumber, RecordAppendOptions, ReservationCollection, AsyncCallback, Object) Begins an asynchronous append operation using space previously reserved in the sequence. This method cannot be inherited.
Public method BeginAppend(IList<ArraySegment<Byte>>, SequenceNumber, SequenceNumber, RecordAppendOptions, ReservationCollection, AsyncCallback, Object) Begins an asynchronous append operation using space previously reserved in the sequence. This method cannot be inherited.
Public method BeginFlush Begins an asynchronous flush operation, using space previously reserved in the sequence. This method cannot be inherited.
Public method BeginReserveAndAppend(ArraySegment<Byte>, SequenceNumber, SequenceNumber, RecordAppendOptions, ReservationCollection, Int64[], AsyncCallback, Object) Begins an asynchronous reserve and append operation. This method cannot be inherited.
Public method BeginReserveAndAppend(IList<ArraySegment<Byte>>, SequenceNumber, SequenceNumber, RecordAppendOptions, ReservationCollection, Int64[], AsyncCallback, Object) Begins an asynchronous reserve and append operation. This method cannot be inherited.
Public method BeginWriteRestartArea(ArraySegment<Byte>, SequenceNumber, ReservationCollection, AsyncCallback, Object) Begins an asynchronous restart area write operation, using space previously reserved in the sequence. This method cannot be inherited.
Public method BeginWriteRestartArea(IList<ArraySegment<Byte>>, SequenceNumber, ReservationCollection, AsyncCallback, Object) Begins an asynchronous restart area write operation, using space previously reserved in the sequence. This method cannot be inherited.
Public method CreateReservationCollection Creates a new ReservationCollection. This method cannot be inherited.
Public method Dispose Immediately releases the unmanaged resources used by an object.
Public method EndAppend Ends an asynchronous append operation. This method cannot be inherited.
Public method EndFlush Ends an asynchronous flush operation. This method cannot be inherited.
Public method EndReserveAndAppend Ends an asynchronous reserve and append operation. This method cannot be inherited.
Public method EndWriteRestartArea Ends an asynchronous restart area write operation. This method cannot be inherited.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method Flush() Ensures that all appended records have been written. This method cannot be inherited.
Public method Flush(SequenceNumber) Ensures that all appended records have been written. This method cannot be inherited.
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ReadLogRecords Returns an enumerable collection of records in the sequence. This method cannot be inherited.
Public method ReadRestartAreas Returns an enumerable collection of the restart areas in the sequence. This method cannot be inherited.
Public method ReserveAndAppend(ArraySegment<Byte>, SequenceNumber, SequenceNumber, RecordAppendOptions, ReservationCollection, Int64[]) Automatically makes a single reservation and appends a record to the sequence. This method cannot be inherited.
Public method ReserveAndAppend(IList<ArraySegment<Byte>>, SequenceNumber, SequenceNumber, RecordAppendOptions, ReservationCollection, Int64[]) Automatically makes a single reservation and appends a record to the sequence. This method cannot be inherited.
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Public method WriteRestartArea(ArraySegment<Byte>) Writes a restart area to the FileRecordSequence. This method cannot be inherited.
Public method WriteRestartArea(IList<ArraySegment<Byte>>) Writes a restart area to the FileRecordSequence. This method cannot be inherited.
Public method WriteRestartArea(ArraySegment<Byte>, SequenceNumber) Writes a restart area to the FileRecordSequence. This method cannot be inherited.
Public method WriteRestartArea(IList<ArraySegment<Byte>>, SequenceNumber) Writes a restart area to the FileRecordSequence. This method cannot be inherited.
Public method WriteRestartArea(ArraySegment<Byte>, SequenceNumber, ReservationCollection) Writes a restart area to the FileRecordSequence. This method cannot be inherited.
Public method WriteRestartArea(IList<ArraySegment<Byte>>, SequenceNumber, ReservationCollection) Writes a restart area to the FileRecordSequence. This method cannot be inherited.
Top
  Name Description
Public event TailPinned Occurs when the record sequence determines that the tail must be moved forward. This method cannot be inherited.
Top

The FileRecordSequence is a record sequence based on a single log file in the file system. It is a simple implementation of the IRecordSequence interface, on top of a simple file-based log.

To manipulate a file-based log, the account that your application is running in must have sufficient privileges, as dictated by the file system security at the time the FileRecordSequence is constructed. In addition, a demand for FullTrust is made at construction time. The permission verification results are cached thereafter in keeping with the Windows security model. You should ensure that you do not accidentally expose the contents of the record sequence to an unauthorized user.

The following example creates a record sequence, appends record to it, and finally reads the records.



	public class MyLog
	{
		string logName = "test.log";
		FileRecordSequence sequence = null;
		bool delete = true;

		public MyLog()
		{
	    // Create a FileRecordSequence.
			sequence = new FileRecordSequence(logName, FileAccess.ReadWrite);
		}

	// Append records to the record sequence.
		public void AppendRecords()
		{
			Console.WriteLine("Appending Log Records...");
			SequenceNumber previous = SequenceNumber.Invalid;

			previous = sequence.Append(CreateData("Hello World!"), SequenceNumber.Invalid, SequenceNumber.Invalid, RecordAppendOptions.ForceFlush);
			previous = sequence.Append(CreateData("This is my first Logging App"), SequenceNumber.Invalid, SequenceNumber.Invalid, RecordAppendOptions.ForceFlush);
			previous = sequence.Append(CreateData("Using FileRecordSequence..."), SequenceNumber.Invalid, SequenceNumber.Invalid, RecordAppendOptions.ForceFlush);

			Console.WriteLine("Done...");
		}
		 
	// Read the records added to the log. 
		public void ReadRecords()
		{
			Encoding enc = Encoding.Unicode;

			Console.WriteLine();

			Console.WriteLine("Reading Log Records...");
			try
			{
				foreach (LogRecord record in this.sequence.ReadLogRecords(this.sequence.BaseSequenceNumber, LogRecordEnumeratorType.Next))
				{
					byte[] data = new byte[record.Data.Length];
					record.Data.Read(data, 0, (int)record.Data.Length);
					string mystr = enc.GetString(data);
					Console.WriteLine("    {0}", mystr);
				}
			}
			catch (Exception e)
			{
				Console.WriteLine("Exception {0} {1}", e.GetType(), e.Message);
			}

			Console.WriteLine();
		}

	// Dispose the record sequence and delete the log file. 
		public void Cleanup()
		{
	    // Dispose the sequence.
			sequence.Dispose();

	    // Delete the log file.
			if (delete)
			{
				try
				{
					File.Delete(this.logName);
				}
				catch (Exception e)
				{
					Console.WriteLine("Exception {0} {1}", e.GetType(), e.Message);
				}
			}
		}

	// Converts the given data to an Array of ArraySegment<byte> 
		public static IList<ArraySegment<byte>> CreateData(string str)
		{
			Encoding enc = Encoding.Unicode;

			byte[] array = enc.GetBytes(str);

			ArraySegment<byte>[] segments = new ArraySegment<byte>[1];
			segments[0] = new ArraySegment<byte>(array);

			return Array.AsReadOnly<ArraySegment<byte>>(segments);
		}
	}

	class LogSample
	{
		static void Main2(string[] args)
		{
			MyLog log = new MyLog();

			log.AppendRecords();
			log.ReadRecords();
			log.Cleanup();
		}
	}


.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ