FileRecordSequence Class

FileRecordSequence Class

 

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

Namespace:   System.IO.Log
Assembly:  System.IO.Log (in System.IO.Log.dll)

System.Object
  System.IO.Log.FileRecordSequence

public sealed class FileRecordSequence : IRecordSequence, IDisposable

NameDescription
System_CAPS_pubmethodFileRecordSequence(String)

Initializes a new instance of the FileRecordSequence class with a specified file.

System_CAPS_pubmethodFileRecordSequence(String, FileAccess)

Initializes a new instance of the FileRecordSequence class with a specified file and an access mode.

System_CAPS_pubmethodFileRecordSequence(String, FileAccess, Int32)

Initializes a new instance of the FileRecordSequence class with a specified file, an access mode, and a file size.

NameDescription
System_CAPS_pubpropertyBaseSequenceNumber

Gets the sequence number of the first valid record in the current FileRecordSequence.

System_CAPS_pubpropertyLastSequenceNumber

Gets the sequence number which is greater than the last record appended.

System_CAPS_pubpropertyMaximumRecordLength

Gets the size of the largest record that can be appended to or read from this sequence, in bytes.

System_CAPS_pubpropertyReservedBytes

Gets the total number of bytes that have been reserved.

System_CAPS_pubpropertyRestartSequenceNumber

Gets the sequence number of the most recently written restart area.

System_CAPS_pubpropertyRetryAppend

Gets or sets a value indicating whether or not appends are automatically retried if the log is full.

NameDescription
System_CAPS_pubmethodAdvanceBaseSequenceNumber(SequenceNumber)

Moves the base sequence number of the log forward. This method cannot be inherited.

System_CAPS_pubmethodAppend(ArraySegment<Byte>, SequenceNumber, SequenceNumber, RecordAppendOptions)

Writes a log record to the FileRecordSequence. This method cannot be inherited.

System_CAPS_pubmethodAppend(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.

System_CAPS_pubmethodAppend(IList<ArraySegment<Byte>>, SequenceNumber, SequenceNumber, RecordAppendOptions)

Writes a log record to the FileRecordSequence. This method cannot be inherited.

System_CAPS_pubmethodAppend(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.

System_CAPS_pubmethodBeginAppend(ArraySegment<Byte>, SequenceNumber, SequenceNumber, RecordAppendOptions, AsyncCallback, Object)

Begins an asynchronous append operation. This method cannot be inherited.

System_CAPS_pubmethodBeginAppend(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.

System_CAPS_pubmethodBeginAppend(IList<ArraySegment<Byte>>, SequenceNumber, SequenceNumber, RecordAppendOptions, AsyncCallback, Object)

Begins an asynchronous append operation. This method cannot be inherited.

System_CAPS_pubmethodBeginAppend(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.

System_CAPS_pubmethodBeginFlush(SequenceNumber, AsyncCallback, Object)

Begins an asynchronous flush operation, using space previously reserved in the sequence. This method cannot be inherited.

System_CAPS_pubmethodBeginReserveAndAppend(ArraySegment<Byte>, SequenceNumber, SequenceNumber, RecordAppendOptions, ReservationCollection, Int64[], AsyncCallback, Object)

Begins an asynchronous reserve and append operation. This method cannot be inherited.

System_CAPS_pubmethodBeginReserveAndAppend(IList<ArraySegment<Byte>>, SequenceNumber, SequenceNumber, RecordAppendOptions, ReservationCollection, Int64[], AsyncCallback, Object)

Begins an asynchronous reserve and append operation. This method cannot be inherited.

System_CAPS_pubmethodBeginWriteRestartArea(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.

System_CAPS_pubmethodBeginWriteRestartArea(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.

System_CAPS_pubmethodCreateReservationCollection()

Creates a new ReservationCollection. This method cannot be inherited.

System_CAPS_pubmethodDispose()

Immediately releases the unmanaged resources used by an object.

System_CAPS_pubmethodEndAppend(IAsyncResult)

Ends an asynchronous append operation. This method cannot be inherited.

System_CAPS_pubmethodEndFlush(IAsyncResult)

Ends an asynchronous flush operation. This method cannot be inherited.

System_CAPS_pubmethodEndReserveAndAppend(IAsyncResult)

Ends an asynchronous reserve and append operation. This method cannot be inherited.

System_CAPS_pubmethodEndWriteRestartArea(IAsyncResult)

Ends an asynchronous restart area write operation. This method cannot be inherited.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethodFlush()

Ensures that all appended records have been written. This method cannot be inherited.

System_CAPS_pubmethodFlush(SequenceNumber)

Ensures that all appended records have been written. This method cannot be inherited.

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodReadLogRecords(SequenceNumber, LogRecordEnumeratorType)

Returns an enumerable collection of records in the sequence. This method cannot be inherited.

System_CAPS_pubmethodReadRestartAreas()

Returns an enumerable collection of the restart areas in the sequence. This method cannot be inherited.

System_CAPS_pubmethodReserveAndAppend(ArraySegment<Byte>, SequenceNumber, SequenceNumber, RecordAppendOptions, ReservationCollection, Int64[])

Automatically makes a single reservation and appends a record to the sequence. This method cannot be inherited.

System_CAPS_pubmethodReserveAndAppend(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.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

System_CAPS_pubmethodWriteRestartArea(ArraySegment<Byte>)

Writes a restart area to the FileRecordSequence. This method cannot be inherited.

System_CAPS_pubmethodWriteRestartArea(ArraySegment<Byte>, SequenceNumber)

Writes a restart area to the FileRecordSequence. This method cannot be inherited.

System_CAPS_pubmethodWriteRestartArea(ArraySegment<Byte>, SequenceNumber, ReservationCollection)

Writes a restart area to the FileRecordSequence. This method cannot be inherited.

System_CAPS_pubmethodWriteRestartArea(IList<ArraySegment<Byte>>)

Writes a restart area to the FileRecordSequence. This method cannot be inherited.

System_CAPS_pubmethodWriteRestartArea(IList<ArraySegment<Byte>>, SequenceNumber)

Writes a restart area to the FileRecordSequence. This method cannot be inherited.

System_CAPS_pubmethodWriteRestartArea(IList<ArraySegment<Byte>>, SequenceNumber, ReservationCollection)

Writes a restart area to the FileRecordSequence. This method cannot be inherited.

NameDescription
System_CAPS_pubeventTailPinned

Occurs when the record sequence determines that the tail must be moved forward. This method cannot be inherited.

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
Available since 3.0

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show:
© 2016 Microsoft