FileRecordSequence 類別

定義

在檔案頂端實作 IRecordSequenceImplements an IRecordSequence on top of a file. 這個類別無法被繼承。This class cannot be inherited.

public ref class FileRecordSequence sealed : IDisposable, System::IO::Log::IRecordSequence
public sealed class FileRecordSequence : IDisposable, System.IO.Log.IRecordSequence
type FileRecordSequence = class
    interface IRecordSequence
    interface IDisposable
Public NotInheritable Class FileRecordSequence
Implements IDisposable, IRecordSequence
繼承
FileRecordSequence
實作

範例

下列範例會建立記錄順序,並附加記錄到其中,最後再讀取記錄。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();
    }
}

Public Class MyLog
    Private logName As String = "test.log"
    Private sequence As FileRecordSequence = Nothing
    Private delete As Boolean = True

    Public Sub New()
    ' Create a FileRecordSequence.
        sequence = New FileRecordSequence(logName, FileAccess.ReadWrite)
    End Sub

' 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

' Read the records added to the log. 
    Public Sub ReadRecords()
        Dim enc As Encoding = Encoding.Unicode

        Console.WriteLine()

        Console.WriteLine("Reading Log Records...")
        Try
            For Each record In Me.sequence.ReadLogRecords(Me.sequence.BaseSequenceNumber, LogRecordEnumeratorType.Next)
                Dim data(record.Data.Length - 1) As Byte
                record.Data.Read(data, 0, CInt(Fix(record.Data.Length)))
                Dim mystr As String = enc.GetString(data)
                Console.WriteLine("    {0}", mystr)
            Next record
        Catch e As Exception
            Console.WriteLine("Exception {0} {1}", e.GetType(), e.Message)
        End Try

        Console.WriteLine()
    End Sub

' Dispose the record sequence and delete the log file. 
    Public Sub Cleanup()
    ' Dispose the sequence.
        sequence.Dispose()

    ' Delete the log file.
        If delete Then
            Try
                File.Delete(Me.logName)
            Catch e As Exception
                Console.WriteLine("Exception {0} {1}", e.GetType(), e.Message)
            End Try
        End If
    End Sub

' Converts the given data to an Array of ArraySegment<byte> 
    Public Shared Function CreateData(ByVal str As String) As IList(Of ArraySegment(Of Byte))
        Dim enc As Encoding = Encoding.Unicode

        Dim array() As Byte = enc.GetBytes(str)

        Dim segments(0) As ArraySegment(Of Byte)
        segments(0) = New ArraySegment(Of Byte)(array)

        Return System.Array.AsReadOnly(Of ArraySegment(Of Byte))(segments)
    End Function
End Class

Friend Class LogSample
    Private Shared Sub Main2(ByVal args() As String)
        Dim log As New MyLog()

        log.AppendRecords()
        log.ReadRecords()
        log.Cleanup()
    End Sub
End Class

備註

FileRecordSequence 是根據檔案系統中單一記錄檔的記錄順序。The FileRecordSequence is a record sequence based on a single log file in the file system. 這是 IRecordSequence 介面的簡單實作,其建構在簡單檔案記錄之上。It is a simple implementation of the IRecordSequence interface, on top of a simple file-based log.

若要操作簡單檔案記錄,執行應用程式所使用的帳戶必須依照檔案系統安全性在建構 FileRecordSequence 時的指定,取得足夠的權限。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. 另外,在建構階段中還會要求 FullTrust 權限。In addition, a demand for FullTrust is made at construction time. 在此之後,權限驗證結果會存放在快取中,以便與 Windows 安全性模型保持一致。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.

建構函式

FileRecordSequence(String)

使用指定的檔案,初始化 FileRecordSequence 類別的新執行個體。Initializes a new instance of the FileRecordSequence class with a specified file.

FileRecordSequence(String, FileAccess)

使用指定的檔案和存取模式初始化 FileRecordSequence 類別的新執行個體。Initializes a new instance of the FileRecordSequence class with a specified file and an access mode.

FileRecordSequence(String, FileAccess, Int32)

使用指定的檔案、存取模式和檔案大小初始化 FileRecordSequence 類別的新執行個體。Initializes a new instance of the FileRecordSequence class with a specified file, an access mode, and a file size.

屬性

BaseSequenceNumber

取得目前 FileRecordSequence 中第一個有效記錄的序號。Gets the sequence number of the first valid record in the current FileRecordSequence.

LastSequenceNumber

取得大於上一筆附加記錄的序號。Gets the sequence number which is greater than the last record appended.

MaximumRecordLength

取得可在順序中附加或讀取之最大記錄的大小 (以位元組為單位)。Gets the size of the largest record that can be appended to or read from this sequence, in bytes.

ReservedBytes

取得保留的位元組總數。Gets the total number of bytes that have been reserved.

RestartSequenceNumber

取得最近寫入之重新啟動區域的序號。Gets the sequence number of the most recently written restart area.

RetryAppend

取得或設定值,這個值表示是否在記錄已滿時自動重試附加作業。Gets or sets a value indicating whether or not appends are automatically retried if the log is full.

方法

AdvanceBaseSequenceNumber(SequenceNumber)

將記錄檔的基底序號往前移動。Moves the base sequence number of the log forward. 這個方法無法被繼承。This method cannot be inherited.

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

將記錄寫入 FileRecordSequenceWrites a log record to the FileRecordSequence. 這個方法無法被繼承。This method cannot be inherited.

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

使用之前在順序中保留的空間,將記錄寫入到 FileRecordSequenceWrites 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)

將記錄寫入 FileRecordSequenceWrites a log record to the FileRecordSequence. 這個方法無法被繼承。This method cannot be inherited.

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

使用之前在順序中保留的空間,將記錄寫入到 FileRecordSequenceWrites a log record to the FileRecordSequence, using space previously reserved in the sequence. 這個方法無法被繼承。This method cannot be inherited.

BeginAppend(ArraySegment<Byte>, SequenceNumber, SequenceNumber, RecordAppendOptions, AsyncCallback, Object)

開始執行非同步的附加作業。Begins an asynchronous append operation. 這個方法無法被繼承。This method cannot be inherited.

BeginAppend(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.

BeginAppend(IList<ArraySegment<Byte>>, SequenceNumber, SequenceNumber, RecordAppendOptions, AsyncCallback, Object)

開始執行非同步的附加作業。Begins an asynchronous append operation. 這個方法無法被繼承。This method cannot be inherited.

BeginAppend(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.

BeginFlush(SequenceNumber, AsyncCallback, Object)

使用先前保留在順序中的空間,開始執行非同步的清除作業。Begins an asynchronous flush operation, using space previously reserved in the sequence. 這個方法無法被繼承。This method cannot be inherited.

BeginReserveAndAppend(ArraySegment<Byte>, SequenceNumber, SequenceNumber, RecordAppendOptions, ReservationCollection, Int64[], AsyncCallback, Object)

開始執行非同步的保留和附加作業。Begins an asynchronous reserve and append operation. 這個方法無法被繼承。This method cannot be inherited.

BeginReserveAndAppend(IList<ArraySegment<Byte>>, SequenceNumber, SequenceNumber, RecordAppendOptions, ReservationCollection, Int64[], AsyncCallback, Object)

開始執行非同步的保留和附加作業。Begins an asynchronous reserve and append operation. 這個方法無法被繼承。This method cannot be inherited.

BeginWriteRestartArea(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.

BeginWriteRestartArea(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.

CreateReservationCollection()

建立新的 ReservationCollectionCreates a new ReservationCollection. 這個方法無法被繼承。This method cannot be inherited.

Dispose()

立即釋放物件所使用的 Unmanaged 資源。Immediately releases the unmanaged resources used by an object.

EndAppend(IAsyncResult)

結束執行非同步的附加作業。Ends an asynchronous append operation. 這個方法無法被繼承。This method cannot be inherited.

EndFlush(IAsyncResult)

結束執行非同步的清除作業。Ends an asynchronous flush operation. 這個方法無法被繼承。This method cannot be inherited.

EndReserveAndAppend(IAsyncResult)

結束執行非同步的保留和附加作業。Ends an asynchronous reserve and append operation. 這個方法無法被繼承。This method cannot be inherited.

EndWriteRestartArea(IAsyncResult)

結束執行非同步的重新啟動區域寫入作業。Ends an asynchronous restart area write operation. 這個方法無法被繼承。This method cannot be inherited.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
Flush()

確定已寫入所有附加的記錄。Ensures that all appended records have been written. 這個方法無法被繼承。This method cannot be inherited.

Flush(SequenceNumber)

確定已寫入所有附加的記錄。Ensures that all appended records have been written. 這個方法無法被繼承。This method cannot be inherited.

GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
ReadLogRecords(SequenceNumber, LogRecordEnumeratorType)

傳回順序中之記錄的可列舉集合。Returns an enumerable collection of records in the sequence. 這個方法無法被繼承。This method cannot be inherited.

ReadRestartAreas()

傳回序列中重新啟動區域的可列舉集合。Returns an enumerable collection of the restart areas in the sequence. 這個方法無法被繼承。This method cannot be inherited.

ReserveAndAppend(ArraySegment<Byte>, SequenceNumber, SequenceNumber, RecordAppendOptions, ReservationCollection, Int64[])

自動建立單一保留區,並將記錄附加至順序。Automatically makes a single reservation and appends a record to the sequence. 這個方法無法被繼承。This method cannot be inherited.

ReserveAndAppend(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.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)
WriteRestartArea(ArraySegment<Byte>)

將重新啟動區域寫入 FileRecordSequenceWrites a restart area to the FileRecordSequence. 這個方法無法被繼承。This method cannot be inherited.

WriteRestartArea(ArraySegment<Byte>, SequenceNumber)

將重新啟動區域寫入 FileRecordSequenceWrites a restart area to the FileRecordSequence. 這個方法無法被繼承。This method cannot be inherited.

WriteRestartArea(ArraySegment<Byte>, SequenceNumber, ReservationCollection)

將重新啟動區域寫入 FileRecordSequenceWrites a restart area to the FileRecordSequence. 這個方法無法被繼承。This method cannot be inherited.

WriteRestartArea(IList<ArraySegment<Byte>>)

將重新啟動區域寫入 FileRecordSequenceWrites a restart area to the FileRecordSequence. 這個方法無法被繼承。This method cannot be inherited.

WriteRestartArea(IList<ArraySegment<Byte>>, SequenceNumber)

將重新啟動區域寫入 FileRecordSequenceWrites a restart area to the FileRecordSequence. 這個方法無法被繼承。This method cannot be inherited.

WriteRestartArea(IList<ArraySegment<Byte>>, SequenceNumber, ReservationCollection)

將重新啟動區域寫入 FileRecordSequenceWrites a restart area to the FileRecordSequence. 這個方法無法被繼承。This method cannot be inherited.

事件

TailPinned

會在記錄順序判斷必須將結尾往前移動時發生。Occurs when the record sequence determines that the tail must be moved forward. 這個方法無法被繼承。This method cannot be inherited.

適用於