FileRecordSequence 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在文件上实现 IRecordSequence。 此类不能被继承。
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
- 实现
示例
下面的示例创建一个记录序列,向该序列追加记录,最后读取这些记录。
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 是基于文件系统中的单一日志文件的记录序列。 它是 IRecordSequence 接口在基于简单文件的日志之上的简单实现。
若要操作基于文件的日志,运行应用程序时使用的帐户必须具有足够的特权,这与构造 FileRecordSequence 时文件系统安全性所规定的要求是相同的。 此外,在构造时要发出 FullTrust 请求。 此后,要对权限验证结果进行缓存,以便与 Windows 安全模式保持一致。 应该确保您没有意外地向未授权用户公开记录序列的内容。
构造函数
FileRecordSequence(String) |
使用指定的文件初始化 FileRecordSequence 类的新实例。 |
FileRecordSequence(String, FileAccess) |
使用指定的文件和访问模式初始化 FileRecordSequence 类的新实例。 |
FileRecordSequence(String, FileAccess, Int32) |
使用指定的文件、访问模式和文件大小初始化 FileRecordSequence 类的新实例。 |
属性
BaseSequenceNumber |
获取当前 FileRecordSequence 中第一条有效记录的序列号。 |
LastSequenceNumber |
获取比追加的最新记录大的序列号。 |
MaximumRecordLength |
获取可以追加到此序列或者从此序列中读取的最大记录的大小(以字节为单位)。 |
ReservedBytes |
获取已保留的字节总数。 |
RestartSequenceNumber |
获取最近写入的重新开始区域的序列号。 |
RetryAppend |
获取或设置一个值,指示在日志已满时是否自动重试追加操作。 |
方法
事件
TailPinned |
当记录序列确定必须前移尾数据时发生。 无法继承此方法。 |