Export (0) Print
Expand All
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.

  NameDescription
Public methodFileRecordSequence(String)Initializes a new instance of the FileRecordSequence class with a specified file.
Public methodFileRecordSequence(String, FileAccess)Initializes a new instance of the FileRecordSequence class with a specified file and an access mode.
Public methodFileRecordSequence(String, FileAccess, Int32)Initializes a new instance of the FileRecordSequence class with a specified file, an access mode, and a file size.
Top
  NameDescription
Public propertyBaseSequenceNumberGets the sequence number of the first valid record in the current FileRecordSequence.
Public propertyLastSequenceNumberGets the sequence number which is greater than the last record appended.
Public propertyMaximumRecordLengthGets the size of the largest record that can be appended to or read from this sequence, in bytes.
Public propertyReservedBytesGets the total number of bytes that have been reserved.
Public propertyRestartSequenceNumberGets the sequence number of the most recently written restart area.
Public propertyRetryAppendGets or sets a value indicating whether or not appends are automatically retried if the log is full.
Top
  NameDescription
Public methodAdvanceBaseSequenceNumberMoves the base sequence number of the log forward. This method cannot be inherited.
Public methodAppend(ArraySegment<Byte>, SequenceNumber, SequenceNumber, RecordAppendOptions)Writes a log record to the FileRecordSequence. This method cannot be inherited.
Public methodAppend(IList<ArraySegment<Byte>>, SequenceNumber, SequenceNumber, RecordAppendOptions)Writes a log record to the FileRecordSequence. This method cannot be inherited.
Public methodAppend(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 methodAppend(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 methodBeginAppend(ArraySegment<Byte>, SequenceNumber, SequenceNumber, RecordAppendOptions, AsyncCallback, Object)Begins an asynchronous append operation. This method cannot be inherited.
Public methodBeginAppend(IList<ArraySegment<Byte>>, SequenceNumber, SequenceNumber, RecordAppendOptions, AsyncCallback, Object)Begins an asynchronous append operation. This method cannot be inherited.
Public methodBeginAppend(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 methodBeginAppend(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 methodBeginFlushBegins an asynchronous flush operation, using space previously reserved in the sequence. This method cannot be inherited.
Public methodBeginReserveAndAppend(ArraySegment<Byte>, SequenceNumber, SequenceNumber, RecordAppendOptions, ReservationCollection, Int64[], AsyncCallback, Object)Begins an asynchronous reserve and append operation. This method cannot be inherited.
Public methodBeginReserveAndAppend(IList<ArraySegment<Byte>>, SequenceNumber, SequenceNumber, RecordAppendOptions, ReservationCollection, Int64[], AsyncCallback, Object)Begins an asynchronous reserve and append operation. This method cannot be inherited.
Public methodBeginWriteRestartArea(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 methodBeginWriteRestartArea(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 methodCreateReservationCollectionCreates a new ReservationCollection. This method cannot be inherited.
Public methodDisposeImmediately releases the unmanaged resources used by an object.
Public methodEndAppendEnds an asynchronous append operation. This method cannot be inherited.
Public methodEndFlushEnds an asynchronous flush operation. This method cannot be inherited.
Public methodEndReserveAndAppendEnds an asynchronous reserve and append operation. This method cannot be inherited.
Public methodEndWriteRestartAreaEnds an asynchronous restart area write operation. This method cannot be inherited.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodFlush()Ensures that all appended records have been written. This method cannot be inherited.
Public methodFlush(SequenceNumber)Ensures that all appended records have been written. This method cannot be inherited.
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodReadLogRecordsReturns an enumerable collection of records in the sequence. This method cannot be inherited.
Public methodReadRestartAreasReturns an enumerable collection of the restart areas in the sequence. This method cannot be inherited.
Public methodReserveAndAppend(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 methodReserveAndAppend(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 methodToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodWriteRestartArea(ArraySegment<Byte>)Writes a restart area to the FileRecordSequence. This method cannot be inherited.
Public methodWriteRestartArea(IList<ArraySegment<Byte>>)Writes a restart area to the FileRecordSequence. This method cannot be inherited.
Public methodWriteRestartArea(ArraySegment<Byte>, SequenceNumber)Writes a restart area to the FileRecordSequence. This method cannot be inherited.
Public methodWriteRestartArea(IList<ArraySegment<Byte>>, SequenceNumber)Writes a restart area to the FileRecordSequence. This method cannot be inherited.
Public methodWriteRestartArea(ArraySegment<Byte>, SequenceNumber, ReservationCollection)Writes a restart area to the FileRecordSequence. This method cannot be inherited.
Public methodWriteRestartArea(IList<ArraySegment<Byte>>, SequenceNumber, ReservationCollection)Writes a restart area to the FileRecordSequence. This method cannot be inherited.
Top
  NameDescription
Public eventTailPinnedOccurs 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.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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.
Show:
© 2014 Microsoft. All rights reserved.