# The C# LogReader Class

### For an overview see page C# Classes

## Class Definition

        public class LogReader
        {
            // ===== NESTED STRUCT: LogInfo =====
            public struct LogInfo
            {
                public long     StartTransCounter;
                public long     StopTransCounter;
                public long     StoredTransactions;
                public ushort   DiskPageSize;
                public ushort   PageSize;
                public long     LogSize;
                public String[] Labels;
                public bool     CRC;
                public bool     Aligned;
                public bool     DictionaryCompat;
                public bool     TransactionApplyCompat;
                public bool     TransactionAppendCompat;
                public bool     NotCorrupted;
                public bool     Iteratable;
            }

            // ===== INTERFACE: ILogIterator =====
            public interface ILogIterator
            {
                void Create(Object obj);
                void Update(Object obj);
                void Delete(Object obj);
                void DeleteAll(Type type);
                void EndOfTransaction();
            }

            // ===== CONSTRUCTOR =====
            public LogReader(Connection con, string filePath);

            // ===== REPLAY METHODS =====
            public void Apply(int labelId);
            public void Apply();

            // ===== ITERATION METHODS =====
            public void IterateFile(ILogIterator iterator, long memSize, IntPtr hint);
            public void IteratePipe(ILogIterator iterator, long memSize, IntPtr hint);
            public void IteratePipe(ILogIterator iterator, long memSize, IntPtr hint, Database.Device pipe);
            public void StopPipeIteration();

            // ===== INFORMATION METHODS =====
            public LogInfo GetInfo();

            // ===== INTERNAL CALLBACK (do not call) =====
            public int InvokeCallback(ILogIterator iterator, int operation, int classId, IntPtr handle);
        }

## Member Descriptions

LogInfo struct (LogReader)
Metadata about a transaction log file: transaction range (`Start/StopTransCounter`), storage stats (`StoredTransactions`, `LogSize`), format details (`DiskPageSize`, `CRC`, `Aligned`), compatibility flags (`DictionaryCompat`, `TransactionApply/AppendCompat`), integrity status (`NotCorrupted`), and replay capability (`Iteratable`).
ILogIterator interface
Callback interface for processing log entries during iteration. Implement these methods to handle object lifecycle events: `Create`, `Update`, `Delete`, `DeleteAll` (class-level deletion), and `EndOfTransaction` (called when `RT_OPTION_MARK_LAST_OBJ` is set).
LogReader(Connection, string)
Creates a transaction log reader for replaying or inspecting a log file. The connection may point to the target database (for replay) or a pipe database (for real-time synchronization).
Apply(int labelId)
Replays transactions from the log file into the connected database up to the specified label ID. Use `LogWriter.SetLabel()` to create recovery points. Pass `-1` to replay to the end of the log.
Apply()
Overload that replays all transactions in the log file to the end. Equivalent to `Apply(-1)`.
IterateFile(ILogIterator, long, IntPtr)
Iterates through log entries from a file-based log, invoking callback methods on the provided `ILogIterator`. `memSize` specifies the temporary in-memory database size for object reconstruction; `hint` is an optional native hint parameter.
IteratePipe(ILogIterator, long, IntPtr[, Database.Device])
Iterates through log entries streamed from a pipe. Overloads support default pipe, explicit pipe device, or connection-bound pipe. Enables real-time replication and monitoring scenarios.
StopPipeIteration()
Stops an active pipe-based iteration initiated by `IteratePipe()`. Use to gracefully terminate real-time log consumption.
GetInfo()
Returns metadata about the log file via the `LogInfo` structure. Use to verify compatibility, integrity, and available recovery points before replay.
InvokeCallback(...) (internal)
Internal method called by the native layer to dispatch log events to the managed `ILogIterator`. Applications should never call this method directly.
Replay Safety
Before calling `Apply()`, verify `LogInfo.TransactionApplyCompat` and `DictionaryCompat` are `true`. Mismatched database schema or eXtremeDB versions may cause replay failures or data corruption.
Memory Requirements
The `memSize` parameter for iteration methods must be large enough to hold the largest object in the log. Insufficient memory causes iteration to fail with out-of-memory errors.
Label-Based Recovery
Use `LogWriter.SetLabel()` to mark recovery points (e.g., after backups). During recovery, call `LogReader.Apply(labelId)` to replay only transactions up to the desired checkpoint, minimizing recovery time.
Pipe Synchronization
For real-time replication: writer uses `LogFlags.Pipe`, reader connects to the same pipe name. The reader's `IteratePipe()` blocks waiting for new entries, enabling near-zero-RPO replication.
Error Handling
Log iteration callbacks should handle exceptions gracefully. If `InvokeCallback` returns a non-zero error code, iteration stops and the error is propagated to the caller.
Thread Safety
`LogReader` instances are not thread-safe. Use separate reader instances per thread. The underlying connection must not be used concurrently for other database operations during log replay.