當前位置: 首頁>>代碼示例>>C#>>正文


C# FileRecordSequence.Append方法代碼示例

本文整理匯總了C#中System.IO.Log.FileRecordSequence.Append方法的典型用法代碼示例。如果您正苦於以下問題:C# FileRecordSequence.Append方法的具體用法?C# FileRecordSequence.Append怎麽用?C# FileRecordSequence.Append使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.IO.Log.FileRecordSequence的用法示例。


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

示例1: AppendRecord

//引入命名空間
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()); 
        } 
    } 
}
開發者ID:.NET開發者,項目名稱:System.IO.Log,代碼行數:51,代碼來源:FileRecordSequence.Append

示例2: MyLog

//引入命名空間
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();
        }
    }
}
開發者ID:.NET開發者,項目名稱:System.IO.Log,代碼行數:106,代碼來源:FileRecordSequence.Append

示例3: AppendRecords

// 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...");
    }
開發者ID:.NET開發者,項目名稱:System.IO.Log,代碼行數:12,代碼來源:FileRecordSequence.Append


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