LogStore.SetArchiveTail(SequenceNumber) 方法

定义

设置存档尾数据的序列号。

public:
 void SetArchiveTail(System::IO::Log::SequenceNumber archiveTail);
public void SetArchiveTail (System.IO.Log.SequenceNumber archiveTail);
member this.SetArchiveTail : System.IO.Log.SequenceNumber -> unit
Public Sub SetArchiveTail (archiveTail As SequenceNumber)

参数

archiveTail
SequenceNumber

存档尾数据的序列号。

例外

archiveTail 不在此序列的基序列号和最后一个序列号之间。

archiveTail 对此序列无效。

执行了无效的操作。

创建存档快照时发生 I/O 错误。

日志存储区不可存档。

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

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

操作系统拒绝了对指定的日志存储区的访问。

示例

下面的示例演示如何将 LogStore 存档到 XML 文档。

class LogBackup
{
    static void ArchiveToXML(LogStore logStore, string fileName)
    {
        LogArchiveSnapshot snapshot = logStore.CreateLogArchiveSnapshot();

        XmlTextWriter writer = new XmlTextWriter(fileName, Encoding.ASCII);

        writer.WriteStartElement("logArchive");
        foreach(FileRegion region in snapshot.ArchiveRegions)
        {
            writer.WriteStartElement("fileRegion");
            writer.WriteElementString("path", region.Path);
            writer.WriteElementString("length", region.FileLength.ToString());
            writer.WriteElementString("offset", region.Offset.ToString());
            using(Stream dataStream = region.GetStream())
            {
                byte[] data = new byte[dataStream.Length];
                dataStream.Read(data, 0, data.Length);
                writer.WriteElementString("data", Convert.ToBase64String(data));
            }
            writer.WriteEndElement();
        }
        writer.WriteEndElement();
        writer.Close();
        logStore.SetArchiveTail(snapshot.LastSequenceNumber);
    }
    static void RestoreFromXML(string fileName)
    {
        using(XmlTextReader reader = new XmlTextReader(fileName))
        {
            reader.ReadStartElement("logArchive");
            while(reader.IsStartElement())
            {
                string path = reader.ReadElementString("path");
                long length = Int64.Parse(reader.ReadElementString("length"));
                long offset = Int64.Parse(reader.ReadElementString("offset"));
                string dataString = reader.ReadElementString("data");
                byte[] data = Convert.FromBase64String(dataString);
                FileStream fileStream;
                using(fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    fileStream.SetLength(length);
                    fileStream.Position = offset; fileStream.Write(data, 0, data.Length);
                }
            }
        reader.ReadEndElement();
        }
    }
}
Friend Class LogBackup
    Private Shared Sub ArchiveToXML(ByVal logStore As LogStore, ByVal fileName As String)
        Dim snapshot As LogArchiveSnapshot = logStore.CreateLogArchiveSnapshot()

        Dim writer As New XmlTextWriter(fileName, Encoding.ASCII)

        writer.WriteStartElement("logArchive")
        For Each region As FileRegion In snapshot.ArchiveRegions
            writer.WriteStartElement("fileRegion")
            writer.WriteElementString("path", region.Path)
            writer.WriteElementString("length", region.FileLength.ToString())
            writer.WriteElementString("offset", region.Offset.ToString())
            Using dataStream As Stream = region.GetStream()
                Dim data(dataStream.Length - 1) As Byte
                dataStream.Read(data, 0, data.Length)
                writer.WriteElementString("data", Convert.ToBase64String(data))
            End Using
            writer.WriteEndElement()
        Next region
        writer.WriteEndElement()
        writer.Close()
        logStore.SetArchiveTail(snapshot.LastSequenceNumber)

    End Sub
    Private Shared Sub RestoreFromXML(ByVal fileName As String)
        Using reader As New XmlTextReader(fileName)
            reader.ReadStartElement("logArchive")
            Do While reader.IsStartElement()
                Dim path = reader.ReadElementString("path")
                Dim length = Int64.Parse(reader.ReadElementString("length"))
                Dim offset = Int64.Parse(reader.ReadElementString("offset"))
                Dim dataString = reader.ReadElementString("data")
                Dim data() = Convert.FromBase64String(dataString)
                Dim fileStream As FileStream
                fileStream = New FileStream(path, FileMode.OpenOrCreate, FileAccess.Write)
                Using fileStream
                    fileStream.SetLength(length)
                    fileStream.Position = offset
                    fileStream.Write(data, 0, data.Length)
                End Using
            Loop
            reader.ReadEndElement()
        End Using
    End Sub
End Class

注解

基序列号和存档尾数据中的较小者决定日志的尾数据。

适用于