public void Open(string path, string streamId = null) { lock (this.storeLock) { this.logFileBase = path; var logFileName = string.IsNullOrEmpty(streamId) ? path : $"{path}::{streamId}"; this.log = new LogRecordSequence( logFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite) { RetryAppend = true }; this.log.TailPinned += this.TailPinned; if (this.log.LogStore.Extents.Count < 2) { this.log.LogStore.Extents.Add($"{path}.log.0", ExtentSize); this.log.LogStore.Extents.Add($"{path}.log.1"); } this.log.LogStore.Policy.AutoGrow = true; this.log.LogStore.Policy.GrowthRate = new PolicyUnit(5, PolicyUnitType.Extents); this.log.LogStore.Policy.NewExtentPrefix = $"{path}.log."; this.log.LogStore.Policy.NextExtentSuffix = this.log.LogStore.Extents.Count; this.log.LogStore.Policy.MaximumExtentCount = int.MaxValue; this.log.LogStore.Policy.Commit(); this.log.LogStore.Policy.Refresh(); } }
// Demonstrates how to create a sequence static void Setup() { if (usingFileLog) { // Setup a FileRecordSequence sequence = (IRecordSequence) new FileRecordSequence(logPath); Console.WriteLine("Successfully created FileRecordSequence"); } else { // Setup a LogRecordSequence LogRecordSequence lrs = new LogRecordSequence(logPath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None); // Add two extents to the log record sequence // Some LogRecordSequence operations require more than one sequence lrs.LogStore.Extents.Add(logContainer0, containerSize); lrs.LogStore.Extents.Add(logContainer1); sequence = lrs; Console.WriteLine("Successfully created LogRecordSequence"); } // Create a total percentage processor time performance counter cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); cpuCounter.NextValue(); }
internal LogFlushAsyncResult(LogRecordSequence recordSequence, AsyncCallback callback, object state) : base(callback, state) { this.recordSequence = recordSequence; }
public Logging(string sFileName, string sLocation, bool bFileBased) { m_bFileBased = bFileBased; m_sFileName = sFileName; if (sLocation.Length > 0) if (sLocation[sLocation.Length - 1] != '\\') sLocation += "\\"; if (m_bFileBased) { m_LogFS = OpenFile(sLocation, m_sFileName, m_sFileExt); } else { string sPath = sLocation + m_sFileName + m_sFileExt; m_LogRS = new LogRecordSequence(sPath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None); m_LogRS.LogStore.Extents.Add("app_extent0", 32 * 1024); m_LogRS.LogStore.Extents.Add("app_extent1"); // Set up auto-grow and such m_LogRS.LogStore.Policy.AutoGrow = true; m_LogRS.LogStore.Policy.MaximumExtentCount = 6; m_LogRS.LogStore.Policy.GrowthRate = new PolicyUnit(5, PolicyUnitType.Extents); m_LogRS.LogStore.Policy.Commit(); m_LogRS.LogStore.Policy.Refresh(); } // Set up Log(Logging.LOGTYPE.ERROR, "Start logging..."); }
public static Task <SequenceNumber> FlushAsync(this LogRecordSequence sequence, SequenceNumber sequenceNumber) { return (Task.Factory.FromAsync( (callback, state) => ((LogRecordSequence)state).BeginFlush(sequenceNumber, callback, state), sequence.EndFlush, sequence)); }
internal LogWriteRestartAreaAsyncResult(LogRecordSequence recordSequence, AsyncCallback callback, object state) : base(callback, state) { this.state = new LogWriteRestartAreaState(); this.state.RecordSequence = recordSequence; this.state.AsyncResult = this; }
internal LogAppendAsyncResult(LogRecordSequence recordSequence, AsyncCallback callback, object state) : base(callback, state) { this.state = new LogReserveAndAppendState(); this.state.RecordSequence = recordSequence; this.state.AsyncResult = this; }
public MyLog() { // Create a LogRecordSequence. sequence = new LogRecordSequence(this.logName, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None); // At least one container/extent must be added for Log Record Sequence. sequence.LogStore.Extents.Add(this.logContainer, this.containerSize); MySequence = sequence; }
/// <summary> /// Reserves and appends a set of records. /// </summary> /// <param name="sequence">The log record sequence.</param> /// <param name="data">Byte array segments that will be concatenated and appended as the record.</param> /// <param name="newBaseSeqNum">The new base sequence number. The specified sequence number must be greater than or equal to the current base sequence number.</param> /// <param name="reservationCollection">The reservation collection to make reservations in.</param> /// <returns>The sequence number of the appended log record.</returns> public static Task <SequenceNumber> WriteRestartArea( this LogRecordSequence sequence, IList <ArraySegment <byte> > data, SequenceNumber newBaseSeqNum, ReservationCollection reservationCollection) { return (Task.Factory.FromAsync( (callback, state) => ((LogRecordSequence)state).BeginWriteRestartArea( data, newBaseSeqNum, reservationCollection, callback, state), sequence.EndWriteRestartArea, sequence)); }
public static Task <SequenceNumber> AppendAsync( this LogRecordSequence sequence, ArraySegment <byte> data, SequenceNumber nextUndoRecord, SequenceNumber previousRecord, RecordAppendOptions recordAppendOptions) { return (Task.Factory.FromAsync( (callback, state) => ((LogRecordSequence)state).BeginAppend( data, nextUndoRecord, previousRecord, recordAppendOptions, callback, state), sequence.EndAppend, sequence)); }
public static Task <SequenceNumber> AppendAsync( this LogRecordSequence sequence, IList <ArraySegment <byte> > data, SequenceNumber userRecord, SequenceNumber previousRecord, RecordAppendOptions recordAppendOptions, ReservationCollection reservationCollection) { return (Task.Factory.FromAsync( (callback, state) => ((LogRecordSequence)state).BeginAppend( data, userRecord, previousRecord, recordAppendOptions, reservationCollection, callback, state), sequence.EndAppend, sequence)); }
/// <summary> /// Initializes a new instance of the <see cref="Logger"/> class. /// </summary> /// <param name="basePath"> /// The base path where the log files are resided. /// </param> /// <param name="baseName"> /// The base name of the log files. /// </param> /// <param name="extentSize"> /// Size of each log extent in bytes. /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="basePath"/> is null, or <paramref name="baseName"/> is null. /// </exception> public Logger(string basePath, string baseName, int extentSize) { if (string.IsNullOrEmpty(basePath)) { throw new ArgumentNullException("basePath"); } if (string.IsNullOrEmpty(baseName)) { throw new ArgumentNullException("baseName"); } // If the base path doesn't exist, create it. if (!Directory.Exists(basePath)) { Directory.CreateDirectory(basePath); } string fullPath = Path.Combine(basePath, baseName); try { _store = new LogStore(fullPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); } catch (PlatformNotSupportedException) { AutomaticDisable = true; return; } _sequence = new LogRecordSequence(_store); _sequence.RetryAppend = true; if (_store.Extents.Count == 0) { _store.Extents.Add(fullPath, extentSize); } }
static void Main2(string[] args) { try { string myLog = "MyMultiplexLog"; string logStream1 = "MyMultiplexLog::MyLogStream1"; string logStream2 = "MyMultiplexLog::MyLogStream2"; int containerSize = 32 * 1024; LogRecordSequence sequence1 = null; LogRecordSequence sequence2 = null; Console.WriteLine("Creating Multiplexed log with two streams"); // <Snippet11> // Create log stream 1. sequence1 = new LogRecordSequence(logStream1, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); // Log Extents are shared between the two streams. // Add two extents to sequence1. sequence1.LogStore.Extents.Add("MyExtent0", containerSize); sequence1.LogStore.Extents.Add("MyExtent1"); // Create log stream 2. sequence2 = new LogRecordSequence(logStream2, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); // </Snippet11> // <Snippet13> // Start Appending in two streams with interleaving appends. SequenceNumber previous1 = SequenceNumber.Invalid; SequenceNumber previous2 = SequenceNumber.Invalid; Console.WriteLine("Appending interleaving records in stream1 and stream2..."); Console.WriteLine(); // Append two records in stream1. previous1 = sequence1.Append( CreateData("MyLogStream1: Hello World!"), SequenceNumber.Invalid, SequenceNumber.Invalid, RecordAppendOptions.ForceFlush); previous1 = sequence1.Append( CreateData("MyLogStream1: This is my first Logging App"), previous1, previous1, RecordAppendOptions.ForceFlush); // Append two records in stream2. previous2 = sequence2.Append( CreateData("MyLogStream2: Hello World!"), SequenceNumber.Invalid, SequenceNumber.Invalid, RecordAppendOptions.ForceFlush); previous2 = sequence2.Append( CreateData("MyLogStream2: This is my first Logging App"), previous2, previous2, RecordAppendOptions.ForceFlush); // Append the third record in stream1. previous1 = sequence1.Append(CreateData( "MyLogStream1: Using LogRecordSequence..."), previous1, previous1, RecordAppendOptions.ForceFlush); // Append the third record in stream2. previous2 = sequence2.Append( CreateData("MyLogStream2: Using LogRecordSequence..."), previous2, previous2, RecordAppendOptions.ForceFlush); // </Snippet13> // Read the log records from stream1 and stream2. Encoding enc = Encoding.Unicode; Console.WriteLine(); Console.WriteLine("Reading Log Records from stream1..."); // <Snippet10> foreach (LogRecord record in sequence1.ReadLogRecords(sequence1.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); } // </Snippet10> Console.WriteLine(); Console.WriteLine("Reading the log records from stream2..."); foreach (LogRecord record in sequence2.ReadLogRecords(sequence2.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); } // <Snippet12> Console.WriteLine(); // Cleanup... sequence1.Dispose(); sequence2.Dispose(); // </Snippet12> LogStore.Delete(myLog); Console.WriteLine("Done..."); } catch (Exception e) { Console.WriteLine("Exception thrown {0} {1}", e.GetType(), e.Message); } }
internal LogReservationCollection(LogRecordSequence sequence) { this.recordSequence = sequence; }
// Demonstrates how to create a sequence static void Setup() { if (usingFileLog) { // Setup a FileRecordSequence sequence = (IRecordSequence)new FileRecordSequence(logPath); Console.WriteLine("Successfully created FileRecordSequence"); } else { // Setup a LogRecordSequence LogRecordSequence lrs = new LogRecordSequence(logPath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None); // Add two extents to the log record sequence // Some LogRecordSequence operations require more than one sequence lrs.LogStore.Extents.Add(logContainer0, containerSize); lrs.LogStore.Extents.Add(logContainer1); sequence = lrs; Console.WriteLine("Successfully created LogRecordSequence"); } // Create a total percentage processor time performance counter cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); cpuCounter.NextValue(); }
/// <summary> /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// </summary> public void Dispose() { this.log?.Dispose(); this.log = null; }