using StackExchange.Redis; using System.Text.Json; namespace VanDerHeijden.Logging.Redis; /// /// Writes log entries to a Redis list using RPUSH. /// Each entry is serialized as JSON. The list grows indefinitely unless /// a consumer (e.g. Logstash, a worker service) drains it via LPOP/BLPOP. /// Optionally a TTL can be set to auto-expire the key. /// public sealed class RedisLogWriter( IDatabase database, string listKey = "logs", TimeSpan? ttl = null) : IBatchedLogWriter { private static readonly JsonSerializerOptions JsonOptions = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; /// /// Serializes all entries as JSON and appends them to the Redis list in a single RPUSH command. /// If a TTL is configured, EXPIREAT is applied to the key after each write. /// /// The log entries to push. /// A token that can cancel the operation. public async Task WriteBatchAsync(List entries, CancellationToken ct) { var values = entries .Select(e => (RedisValue)JsonSerializer.Serialize(e, JsonOptions)) .ToArray(); await database.ListRightPushAsync(listKey, values); if (ttl.HasValue) await database.KeyExpireAsync(listKey, ttl.Value); } /// public ValueTask DisposeAsync() => ValueTask.CompletedTask; }