FileRecordSequence.Append 方法

定义

将日志记录写入 FileRecordSequence

重载

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

将日志记录写入 FileRecordSequence。 无法继承此方法。

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

将日志记录写入 FileRecordSequence。 无法继承此方法。

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

使用序列中先前保留的空间将日志记录写入 FileRecordSequence。 无法继承此方法。

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

使用序列中先前保留的空间将日志记录写入 FileRecordSequence。 无法继承此方法。

示例

下面的示例创建一个记录序列,向该序列追加记录,最后读取这些记录。


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)

将日志记录写入 FileRecordSequence。 无法继承此方法。

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

参数

data
ArraySegment<Byte>

将连接在一起并以记录形式追加的字节数组段的列表。

nextUndoRecord
SequenceNumber

用户指定顺序中下一条记录的序列号。

previousRecord
SequenceNumber

“上一个”顺序中下一条记录的序列号。

recordAppendOptions
RecordAppendOptions

RecordAppendOptions 的一个有效值,用于指定应如何写入数据。

返回

追加的日志记录的序列号。

实现

例外

一个或多个参数为 null

一个或多个自变量超出范围。

由于记录序列是通过只读访问权限打开的,因此无法执行该操作。

由于出现意外的 I/O 异常,因此无法执行该请求。

该方法是在序列已释放之后调用的。

没有足够的内存来继续执行该程序。

记录序列已满。

示例

下面的示例创建一个记录序列,向该序列追加记录,最后读取这些记录。

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

注解

包含在 data 参数中的数据将连接成单个字节数组,以便以记录形式追加。 但在读取记录时,无法将数据重新拆分为数组段。

通常,此方法在写入记录之前完成。 若要确保记录已写入,请使用 ForceFlush 参数指定 recordAppendOptions 标志,或者调用 Flush 方法。

适用于

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

将日志记录写入 FileRecordSequence。 无法继承此方法。

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

参数

data
IList<ArraySegment<Byte>>

将连接在一起并以记录形式追加的字节数组段的列表。

nextUndoRecord
SequenceNumber

用户指定顺序中下一条记录的序列号。

previousRecord
SequenceNumber

“上一个”顺序中下一条记录的序列号。

recordAppendOptions
RecordAppendOptions

RecordAppendOptions 的一个有效值,用于指定应如何写入数据。

返回

追加的日志记录的序列号。

实现

例外

一个或多个参数为 null

一个或多个自变量超出范围。

由于记录序列是通过只读访问权限打开的,因此无法执行该操作。

由于出现意外的 I/O 异常,因此无法执行该请求。

该方法是在序列已释放之后调用的。

没有足够的内存来继续执行该程序。

记录序列已满。

示例

下面的示例演示如何使用此方法创建记录序列。

// 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

注解

包含在 data 参数中的数据将连接成单个字节数组,以便以记录形式追加。 但在读取记录时,无法将数据重新拆分为数组段。

通常,此方法在写入记录之前完成。 若要确保记录已写入,请使用 ForceFlush 参数指定 recordAppendOptions 标志,或者调用 Flush 方法。

适用于

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

使用序列中先前保留的空间将日志记录写入 FileRecordSequence。 无法继承此方法。

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

参数

data
ArraySegment<Byte>

将连接在一起并以记录形式追加的字节数组段的列表。

nextUndoRecord
SequenceNumber

用户指定顺序中下一条记录的序列号。

previousRecord
SequenceNumber

“上一个”顺序中下一条记录的序列号。

recordAppendOptions
RecordAppendOptions

RecordAppendOptions 的一个有效值,用于指定应如何写入数据。

reservations
ReservationCollection

一个 ReservationCollection,它包含应当用于此记录的预留。

返回

追加的日志记录的序列号。

实现

例外

一个或多个参数为 null

一个或多个自变量超出范围。

reservations 不是由此记录序列创建的。

由于记录序列是通过只读访问权限打开的,因此无法执行该操作。

由于出现意外的 I/O 异常,因此无法执行该请求。

该方法是在序列已释放之后调用的。

没有足够的内存来继续执行该程序。

记录序列已满。

data 中找不到足以容纳 reservations 的预留。

注解

包含在 data 参数中的数据将连接成单个字节数组,以便以记录形式追加。 但在读取记录时,无法将数据重新拆分为数组段。

追加的记录将使用 reservations 参数指定的预留来占用先前已预留的空间。 如果追加成功,它将占用能容纳数据的最小预留区域,而该预留区域将从集合中移除。

通常,此方法在写入记录之前完成。 若要确保记录已写入,请使用 ForceFlush 参数指定 recordAppendOptions 标志,或者调用 Flush 方法。

适用于

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

使用序列中先前保留的空间将日志记录写入 FileRecordSequence。 无法继承此方法。

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

参数

data
IList<ArraySegment<Byte>>

将连接在一起并以记录形式追加的字节数组段的列表。

nextUndoRecord
SequenceNumber

用户指定顺序中下一条记录的序列号。

previousRecord
SequenceNumber

“上一个”顺序中下一条记录的序列号。

recordAppendOptions
RecordAppendOptions

RecordAppendOptions 的一个有效值,用于指定应如何写入数据。

reservations
ReservationCollection

一个 ReservationCollection,它包含应当用于此记录的预留。

返回

追加的日志记录的序列号。

实现

例外

一个或多个参数为 null

一个或多个自变量超出范围。

reservations 不是由此记录序列创建的。

由于记录序列是通过只读访问权限打开的,因此无法执行该操作。

由于出现意外的 I/O 异常,因此无法执行该请求。

该方法是在序列已释放之后调用的。

没有足够的内存来继续执行该程序。

记录序列已满。

data 中找不到足以容纳 reservations 的预留。

注解

包含在 data 参数中的数据将连接成单个字节数组,以便以记录形式追加。 但在读取记录时,无法将数据重新拆分为数组段。

追加的记录将使用 reservations 参数指定的预留来占用先前已预留的空间。 如果追加成功,它将占用能容纳数据的最小预留区域,而该预留区域将从集合中移除。

通常,此方法在写入记录之前完成。 若要确保记录已写入,请使用 ForceFlush 参数指定 recordAppendOptions 标志,或者调用 Flush 方法。

适用于