共用方式為


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 方法。

適用於