# The C# LogWriter Class

### For an overview see page C# Classes

## Class Definition

        [Flags]
        public enum LogFlags : ushort
        {
            Aligned          = 0x01,   // Start each record with new disk page
            Append           = 0x02,   // Append to an existing log file
            Crc              = 0x04,   // Cover transaction data with CRC32
            SizeCallback     = 0x08,   // User callback to limit size (not supported in .NET)
            SyncInstantly    = 0x10,   // Do disk flush on each record
            SyncTimer        = 0x20,   // Do disk flush by timer
            SyncCount        = 0x40,   // Do disk flush by record count
            Iterable         = 0x80,   // Make log applicable for LogReader.LogIterate
            Restart          = 0x100,  // Stop previous log at once
            Pipe             = 0x200,  // Use pipe interface instead of file
            DynamicPipe      = 0x1000  // Support for dynamic pipes
        }

        public class LogWriter
        {
            // ===== NESTED STRUCT: LogParams =====
            public struct LogParams
            {
                public LogFlags Flags;
                public ushort   DiskPageSize;
                public ushort   FlushDepth;
                public TimeSpan FlushTime;
            }

            // ===== NESTED STRUCT: LogInfo =====
            public struct LogInfo
            {
                public LogFlags Flags;
                public long     StartTransCounter;
                public long     LastTransCounter;
                public long     StoredTransactions;
                public long     LogSize;
                public long     PipeUsedSize;
                public int      LabelsCount;
                public uint     MaxParallelTrans;
                public uint     ExceededTransSlotsCount;
                public long     MaxTransactionSize;
            }

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

            // ===== LIFECYCLE METHODS =====
            public void Start(LogParams logParams);
            public void Stop();
            public void Terminate();
            public void Truncate();

            // ===== LOG MANAGEMENT METHODS =====
            public int SetLabel(string label);
            public bool SaveSnapshot(String databaseSnapshotFilePath);
            public void Flush();

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

## Member Descriptions

LogFlags enum
Flags controlling transaction log behavior: `Aligned` (page-aligned records), `Append` (continue existing log), `Crc` (CRC32 checksums), `SyncInstantly/Timer/Count` (flush policies), `Iterable` (enable replay), `Pipe/DynamicPipe` (pipe-based I/O), `Restart` (replace existing log).
LogParams struct
Configuration container for starting a transaction log: `Flags` (behavior modifiers), `DiskPageSize` (I/O block size), `FlushDepth` (max unflushed records), `FlushTime` (timer-based flush interval).
LogInfo struct (LogWriter)
Runtime statistics for an active log: transaction counters (`Start/LastTransCounter`), storage metrics (`StoredTransactions`, `LogSize`), pipe usage (`PipeUsedSize`), label count, MVCC parallelism stats (`MaxParallelTrans`, `ExceededTransSlotsCount`), and largest transaction size.
LogWriter(Connection, string)
Creates a transaction log writer for the specified database connection and file path. The database must be opened with `Database.Mode.TransactionLoggingSupport` flag.
Start(LogParams)
Begins transaction logging with the specified parameters. All subsequent transactions on this connection are recorded to the log file until `Stop()` is called.
Stop()
Stops logging and closes the log file gracefully. Ensures all buffered data is flushed to disk.
Terminate()
Forcibly terminates logging if the reader in pipe mode becomes unresponsive. Use with caution as it may leave the log in an incomplete state.
SetLabel(string)
Marks a named recovery point in the log at the current position. Returns a numeric label ID that can be used with `LogReader.Apply(labelId)` to replay transactions up to this point.
Truncate()
Clears the current log and restarts logging from scratch. Equivalent to calling `Stop()` followed by `Start()` with the same parameters.
SaveSnapshot(String)
Atomically saves a database snapshot to the specified file and truncates the log. The snapshot can later be loaded via `Database.Open()` with `Database.Parameters.databaseSnapshotFilePath`. Returns `true` on success.
Flush()
Forces immediate flushing of all buffered log data to disk, regardless of configured flush policies. Useful for ensuring durability before critical operations.
GetInfo()
Returns current statistics and configuration of the active log via the `LogInfo` structure.
Logging Requirements
The database must be created or opened with the `Database.Mode.TransactionLoggingSupport` flag. Logging is connection-specific: each `LogWriter` instance manages logging for its associated `Connection`.
Flush Policies
Configure durability vs. performance trade-offs via `LogParams`: `SyncInstantly` (maximum durability, lowest throughput), `SyncTimer` (periodic flushes), `SyncCount` (flush after N records), or combine flags for hybrid behavior.
Pipe Mode
When using `LogFlags.Pipe` or `DynamicPipe`, the log is written to a named pipe instead of a file, enabling real-time replication to a remote `LogReader`. Ensure the reader is started before the writer to avoid data loss.
Recovery Workflow
Typical usage: `Start()` → perform transactions → `SetLabel("checkpoint")` → `SaveSnapshot("backup.db")` → continue → on recovery: load snapshot → `LogReader.Apply(labelId)` to replay to desired point.