Streamfab.keepstreams.generic.hook-smeagol-ther... Apr 2026

public sealed class LoggingHook : IStreamHook { public void BeforeRead(IHookContext ctx, byte[] buffer, int offset, int count) => Console.WriteLine($"[LOG] About to read

using System; using System.IO; using System.IO.Compression; using System.Threading.Tasks; using StreamFab.KeepStreams.Generic;

// 2. The inner stream performs the real read int bytesRead = _inner.Read(buffer, offset, count); StreamFab.KeepStreams.Generic.Hook-Smeagol-TheR...

// 2. Actual read from inner stream int bytesRead = await _inner.ReadAsync(destination, cancellationToken) .ConfigureAwait(false);

| Responsibility | Why it matters | |----------------|----------------| | inbound/outbound data flowing through any System.IO.Stream ‑derived object without breaking the original contract. | Enables logging, diagnostics, transformation, or throttling of data pipelines (e.g., network sockets, file streams, compression streams). | | Preserve the original stream’s semantics (async/sync, seeking, length, timeouts). | Guarantees drop‑in replacement – callers do not need to change their code. | | Compose multiple hooks (e.g., logging + encryption + compression) in a deterministic order. | Keeps the pipeline modular and testable. | | Dispose safely – the hook forwards Dispose / DisposeAsync while also releasing its own resources (buffers, diagnostic listeners). | Prevents resource leaks in long‑running services. | public sealed class LoggingHook : IStreamHook { public

| Event name | Payload | |------------|---------| | ReadStart | StreamId, Count, Timestamp | | ReadStop | StreamId, BytesRead, ElapsedMs | | WriteStart | StreamId, Count, Timestamp | | WriteStop | StreamId, BytesWritten, ElapsedMs | | Error | StreamId, Exception, Operation |

// Then the inner stream is disposed (unless the hook says otherwise) _inner.Dispose(); base.Dispose(disposing); | | Compose multiple hooks (e

// 1. Hook gets a chance to modify the request (e.g., apply a read‑limit) _hook.BeforeRead(_ctx, buffer, offset, count);

private readonly Stream _inner; private readonly THook _hook; private readonly IHookContext _ctx; // …

Request a Quote Today!

Ready to scale with CloudStack HCI? Request your custom quote today.