當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET FileRecordSequence類代碼示例

本文整理匯總了VB.NET中System.IO.Log.FileRecordSequence的典型用法代碼示例。如果您正苦於以下問題:VB.NET FileRecordSequence類的具體用法?VB.NET FileRecordSequence怎麽用?VB.NET FileRecordSequence使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了FileRecordSequence類的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。

示例1: MyLog

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
開發者ID:VB.NET開發者,項目名稱:System.IO.Log,代碼行數:80,代碼來源:FileRecordSequence


注:本文中的System.IO.Log.FileRecordSequence類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。