Edit

Share via


FileRecordSequence.Append Method

Definition

Writes a log record to the FileRecordSequence.

Overloads

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

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

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

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

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.

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.

Examples

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


using System;
using System.IO;
using System.IO.Log;
using System.Collections.Generic;
using System.Text;

namespace MyFileRecordSequence
{

class ReadRecordsSample
{
    static SequenceNumber AppendRecord(IRecordSequence sequence, string message, SequenceNumber user, SequenceNumber previous)
    {
        MemoryStream data = new MemoryStream();
        BinaryWriter writer = new BinaryWriter(data);
        writer.Write(message); ArraySegment<byte>[] segments;
        segments = new ArraySegment<byte>[1];
        segments[0] = new ArraySegment<byte>(data.GetBuffer(), 0, (int)data.Length);
        return sequence.Append(segments, user, previous,RecordAppendOptions.None);
    }
    public static void Main(string[] args)
    {
        IRecordSequence sequence;
        sequence = new FileRecordSequence(args[0]);
        SequenceNumber a, b, c, d;
        a = AppendRecord(sequence, "This is record A", SequenceNumber.Invalid, SequenceNumber.Invalid);
        Console.WriteLine("Record A has sequence number System.IO.Log", a);
        b = AppendRecord(sequence, "This is record B", a, a);
        Console.WriteLine("Record B has sequence number System.IO.Log", b);
        c = AppendRecord(sequence, "This is record C", a, a);
        Console.WriteLine("Record C has sequence number System.IO.Log", c);
        d = AppendRecord(sequence, "This is record D", b, c);
        Console.WriteLine("Record D has sequence number System.IO.Log", d);
        foreach(LogRecord record in sequence.ReadLogRecords(a,LogRecordEnumeratorType.Next))
        {
            BinaryReader reader = new BinaryReader(record.Data);
            Console.WriteLine("System.IO.Log: T:System.IO.Log.IRecordSequence", record.SequenceNumber, reader.ReadString());
        }
        foreach(LogRecord record in sequence.ReadLogRecords(d, LogRecordEnumeratorType.User))
        {
            BinaryReader reader = new BinaryReader(record.Data);
            Console.WriteLine("System.IO.Log: T:System.IO.Log.IRecordSequence", record.SequenceNumber, reader.ReadString());
        }
        foreach(LogRecord record in sequence.ReadLogRecords(d, LogRecordEnumeratorType.Previous))
        {
            BinaryReader reader = new BinaryReader(record.Data);
            Console.WriteLine("System.IO.Log: T:System.IO.Log.IRecordSequence", record.SequenceNumber, reader.ReadString());
        }
    }
}
Imports System.IO
Imports System.IO.Log
Imports System.Collections.Generic
Imports System.Text


Namespace MyFileRecordSequence


Friend Class ReadRecordsSample
    Private Shared Function AppendRecord(ByVal sequence As IRecordSequence, ByVal message As String, ByVal user As SequenceNumber, ByVal previous As SequenceNumber) As SequenceNumber
        Dim data As New MemoryStream()
        Dim writer As New BinaryWriter(data)
        writer.Write(message)
        Dim segments() As ArraySegment(Of Byte)
        segments = New ArraySegment(Of Byte)(0){}
        segments(0) = New ArraySegment(Of Byte)(data.GetBuffer(), 0, CInt(Fix(data.Length)))
        Return sequence.Append(segments, user, previous,RecordAppendOptions.None)
    End Function
    Public Shared Sub Main(ByVal args() As String)
        Dim sequence As IRecordSequence
        sequence = New FileRecordSequence(args(0))
        Dim a, b, c, d As SequenceNumber
        a = AppendRecord(sequence, "This is record A", SequenceNumber.Invalid, SequenceNumber.Invalid)
        Console.WriteLine("Record A has sequence number System.IO.Log", a)
        b = AppendRecord(sequence, "This is record B", a, a)
        Console.WriteLine("Record B has sequence number System.IO.Log", b)
        c = AppendRecord(sequence, "This is record C", a, a)
        Console.WriteLine("Record C has sequence number System.IO.Log", c)
        d = AppendRecord(sequence, "This is record D", b, c)
        Console.WriteLine("Record D has sequence number System.IO.Log", d)
            For Each record In sequence.ReadLogRecords(a, LogRecordEnumeratorType.Next)
                Dim reader As New BinaryReader(record.Data)
                Console.WriteLine("System.IO.Log: T:System.IO.Log.IRecordSequence", record.SequenceNumber, reader.ReadString())
            Next record
            For Each record In sequence.ReadLogRecords(d, LogRecordEnumeratorType.User)
                Dim reader As New BinaryReader(record.Data)
                Console.WriteLine("System.IO.Log: T:System.IO.Log.IRecordSequence", record.SequenceNumber, reader.ReadString())
            Next record
            For Each record In sequence.ReadLogRecords(d, LogRecordEnumeratorType.Previous)
                Dim reader As New BinaryReader(record.Data)
                Console.WriteLine("System.IO.Log: T:System.IO.Log.IRecordSequence", record.SequenceNumber, reader.ReadString())
            Next record
    End Sub
End Class

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

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

public:
 virtual System::IO::Log::SequenceNumber Append(ArraySegment<System::Byte> data, System::IO::Log::SequenceNumber nextUndoRecord, System::IO::Log::SequenceNumber previousRecord, System::IO::Log::RecordAppendOptions recordAppendOptions);
public System.IO.Log.SequenceNumber Append (ArraySegment<byte> data, System.IO.Log.SequenceNumber nextUndoRecord, System.IO.Log.SequenceNumber previousRecord, System.IO.Log.RecordAppendOptions recordAppendOptions);
abstract member Append : ArraySegment<byte> * System.IO.Log.SequenceNumber * System.IO.Log.SequenceNumber * System.IO.Log.RecordAppendOptions -> System.IO.Log.SequenceNumber
override this.Append : ArraySegment<byte> * System.IO.Log.SequenceNumber * System.IO.Log.SequenceNumber * System.IO.Log.RecordAppendOptions -> System.IO.Log.SequenceNumber
Public Function Append (data As ArraySegment(Of Byte), nextUndoRecord As SequenceNumber, previousRecord As SequenceNumber, recordAppendOptions As RecordAppendOptions) As SequenceNumber

Parameters

data
ArraySegment<Byte>

A list of byte array segments that will be concatenated and appended as the record.

nextUndoRecord
SequenceNumber

The sequence number of the next record in the user-specified order.

previousRecord
SequenceNumber

The sequence number of the next record in Previous order.

recordAppendOptions
RecordAppendOptions

A valid value of RecordAppendOptions that specifies how the data should be written.

Returns

The sequence number of the appended log record.

Implements

Exceptions

One or more of the arguments are null.

One or more of the arguments are out of range.

The operation cannot be performed because the record sequence was opened with read-only access.

The request could not be performed because of an unexpected I/O exception.

The method was called after the sequence has been disposed of.

There is not enough memory to continue the execution of the program.

The record sequence is full.

Examples

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

using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.IO.Log;

namespace MyFileRecordSequence
{
    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 Array of ArraSegment<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 Main(string[] args)
        {
            MyLog log = new MyLog();

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

Remarks

Data contained in the data parameter will be concatenated into a single byte array for appending as the record. However, no provision is made for splitting data back into array segments when the record is read.

Normally, this method completes before the record has been written. To ensure that a record has been written, either specify the ForceFlush flag using the recordAppendOptions parameter, or call the Flush method.

Applies to

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

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

public:
 virtual System::IO::Log::SequenceNumber Append(System::Collections::Generic::IList<ArraySegment<System::Byte>> ^ data, System::IO::Log::SequenceNumber nextUndoRecord, System::IO::Log::SequenceNumber previousRecord, System::IO::Log::RecordAppendOptions recordAppendOptions);
public System.IO.Log.SequenceNumber Append (System.Collections.Generic.IList<ArraySegment<byte>> data, System.IO.Log.SequenceNumber nextUndoRecord, System.IO.Log.SequenceNumber previousRecord, System.IO.Log.RecordAppendOptions recordAppendOptions);
abstract member Append : System.Collections.Generic.IList<ArraySegment<byte>> * System.IO.Log.SequenceNumber * System.IO.Log.SequenceNumber * System.IO.Log.RecordAppendOptions -> System.IO.Log.SequenceNumber
override this.Append : System.Collections.Generic.IList<ArraySegment<byte>> * System.IO.Log.SequenceNumber * System.IO.Log.SequenceNumber * System.IO.Log.RecordAppendOptions -> System.IO.Log.SequenceNumber
Public Function Append (data As IList(Of ArraySegment(Of Byte)), nextUndoRecord As SequenceNumber, previousRecord As SequenceNumber, recordAppendOptions As RecordAppendOptions) As SequenceNumber

Parameters

data
IList<ArraySegment<Byte>>

A list of byte array segments that will be concatenated and appended as the record.

nextUndoRecord
SequenceNumber

The sequence number of the next record in the user-specified order.

previousRecord
SequenceNumber

The sequence number of the next record in Previous order.

recordAppendOptions
RecordAppendOptions

A valid value of RecordAppendOptions that specifies how the data should be written.

Returns

The sequence number of the appended log record.

Implements

Exceptions

One or more of the arguments are null.

One or more of the arguments are out of range.

The operation cannot be performed because the record sequence was opened with read-only access.

The request could not be performed because of an unexpected I/O exception.

The method was called after the sequence has been disposed of.

There is not enough memory to continue the execution of the program.

The record sequence is full.

Examples

The following example shows how you can create a record sequence with this method.

// 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...");
    }
' Append records to the record sequence.
    Public Sub AppendRecords()
        Console.WriteLine("Appending Log Records...")
        Dim previous As SequenceNumber = 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...")
    End Sub

Remarks

Data contained in the data parameter will be concatenated into a single byte array for appending as the record. However, no provision is made for splitting data back into array segments when the record is read.

Normally, this method completes before the record has been written. To ensure that a record has been written, either specify the ForceFlush flag using the recordAppendOptions parameter, or call the Flush method.

Applies to

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:
 virtual System::IO::Log::SequenceNumber Append(ArraySegment<System::Byte> data, System::IO::Log::SequenceNumber nextUndoRecord, System::IO::Log::SequenceNumber previousRecord, System::IO::Log::RecordAppendOptions recordAppendOptions, System::IO::Log::ReservationCollection ^ reservations);
public System.IO.Log.SequenceNumber Append (ArraySegment<byte> data, System.IO.Log.SequenceNumber nextUndoRecord, System.IO.Log.SequenceNumber previousRecord, System.IO.Log.RecordAppendOptions recordAppendOptions, System.IO.Log.ReservationCollection reservations);
abstract member Append : ArraySegment<byte> * System.IO.Log.SequenceNumber * System.IO.Log.SequenceNumber * System.IO.Log.RecordAppendOptions * System.IO.Log.ReservationCollection -> System.IO.Log.SequenceNumber
override this.Append : ArraySegment<byte> * System.IO.Log.SequenceNumber * System.IO.Log.SequenceNumber * System.IO.Log.RecordAppendOptions * System.IO.Log.ReservationCollection -> System.IO.Log.SequenceNumber
Public Function Append (data As ArraySegment(Of Byte), nextUndoRecord As SequenceNumber, previousRecord As SequenceNumber, recordAppendOptions As RecordAppendOptions, reservations As ReservationCollection) As SequenceNumber

Parameters

data
ArraySegment<Byte>

A list of byte array segments that will be concatenated and appended as the record.

nextUndoRecord
SequenceNumber

The sequence number of the next record in the user-specified order.

previousRecord
SequenceNumber

The sequence number of the next record in Previous order.

recordAppendOptions
RecordAppendOptions

A valid value of RecordAppendOptions that specifies how the data should be written.

reservations
ReservationCollection

A ReservationCollection that contains the reservation that should be used for this record.

Returns

The sequence number of the appended log record.

Implements

Exceptions

One or more of the arguments are null.

One or more of the arguments are out of range.

reservations was not created by this record sequence.

The operation cannot be performed because the record sequence was opened with read-only access.

The request could not be performed because of an unexpected I/O exception.

The method was called after the sequence has been disposed of.

There is not enough memory to continue the execution of the program.

The record sequence is full.

No reservation large enough to fit data can be found in reservations.

Remarks

Data contained in the data parameter will be concatenated into a single byte array for appending as the record. However, no provision is made for splitting data back into array segments when the record is read.

The appended record will consume space that has been previously reserved, using a reservation specified by the reservations parameter. If the append succeeds, it will consume the smallest reservation area that can hold the data, and that reservation area will be removed from the collection.

Normally, this method completes before the record has been written. To ensure that a record has been written, either specify the ForceFlush flag using the recordAppendOptions parameter, or call the Flush method.

Applies to

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:
 virtual System::IO::Log::SequenceNumber Append(System::Collections::Generic::IList<ArraySegment<System::Byte>> ^ data, System::IO::Log::SequenceNumber nextUndoRecord, System::IO::Log::SequenceNumber previousRecord, System::IO::Log::RecordAppendOptions recordAppendOptions, System::IO::Log::ReservationCollection ^ reservations);
public System.IO.Log.SequenceNumber Append (System.Collections.Generic.IList<ArraySegment<byte>> data, System.IO.Log.SequenceNumber nextUndoRecord, System.IO.Log.SequenceNumber previousRecord, System.IO.Log.RecordAppendOptions recordAppendOptions, System.IO.Log.ReservationCollection reservations);
abstract member Append : System.Collections.Generic.IList<ArraySegment<byte>> * System.IO.Log.SequenceNumber * System.IO.Log.SequenceNumber * System.IO.Log.RecordAppendOptions * System.IO.Log.ReservationCollection -> System.IO.Log.SequenceNumber
override this.Append : System.Collections.Generic.IList<ArraySegment<byte>> * System.IO.Log.SequenceNumber * System.IO.Log.SequenceNumber * System.IO.Log.RecordAppendOptions * System.IO.Log.ReservationCollection -> System.IO.Log.SequenceNumber
Public Function Append (data As IList(Of ArraySegment(Of Byte)), nextUndoRecord As SequenceNumber, previousRecord As SequenceNumber, recordAppendOptions As RecordAppendOptions, reservations As ReservationCollection) As SequenceNumber

Parameters

data
IList<ArraySegment<Byte>>

A list of byte array segments that will be concatenated and appended as the record.

nextUndoRecord
SequenceNumber

The sequence number of the next record in the user-specified order.

previousRecord
SequenceNumber

The sequence number of the next record in Previous order.

recordAppendOptions
RecordAppendOptions

A valid value of RecordAppendOptions that specifies how the data should be written.

reservations
ReservationCollection

A ReservationCollection that contains the reservation that should be used for this record.

Returns

The sequence number of the appended log record.

Implements

Exceptions

One or more of the arguments are null.

One or more of the arguments are out of range.

reservations was not created by this record sequence.

The operation cannot be performed because the record sequence was opened with read-only access.

The request could not be performed because of an unexpected I/O exception.

The method was called after the sequence has been disposed of.

There is not enough memory to continue the execution of the program.

The record sequence is full.

No reservation large enough to fit data can be found in reservations.

Remarks

Data contained in the data parameter will be concatenated into a single byte array for appending as the record. However, no provision is made for splitting data back into array segments when the record is read.

The appended record will consume space that has been previously reserved, using a reservation specified by the reservations parameter. If the append succeeds, it will consume the smallest reservation area that can hold the data, and that reservation area will be removed from the collection.

Normally, this method completes before the record has been written. To ensure that a record has been written, either specify the ForceFlush flag using the recordAppendOptions parameter, or call the Flush method.

Applies to