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)

public sealed class FileRecordSequence : IRecordSequence, 
	IDisposable

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();
		}
	}

System.Object
  System.IO.Log.FileRecordSequence

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

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft