Path: redb-route/redb.Route/Core/MessageHistory.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Core/MessageHistory.cs
----
using System.Collections.Concurrent;
using System.Text;
using redb.Route.Abstractions;
namespace redb.Route.Core;
///
/// One entry in an exchange's message history: which route/node processed it and how long that took
/// (Apache Camel org.apache.camel.MessageHistory).
///
public sealed record MessageHistoryEntry(string RouteId, string NodeId, string Label, double ElapsedMs);
///
/// Access and formatting for the per-exchange message history (Apache Camel Message History EIP).
///
/// When enabled (globally via RouteEngineOptions.EnableMessageHistory or per route via
/// .MessageHistory()), each node is wrapped so it appends a
/// as the exchange passes through, stored on the exchange under in a
/// thread-safe queue (Split/Multicast append concurrently). The trail is dumped on failure.
///
///
public static class MessageHistory
{
/// Exchange property key holding the message history (Camel: Exchange.MESSAGE_HISTORY).
public const string PropertyKey = "CamelMessageHistory";
/// Returns the recorded history for the exchange (empty if none / disabled).
public static IReadOnlyList GetEntries(IExchange exchange)
=> exchange.Properties.TryGetValue(PropertyKey, out var raw) && raw is ConcurrentQueue q
? q.ToArray()
: Array.Empty();
/// Appends an entry, creating the queue on first use.
internal static void Append(IExchange exchange, MessageHistoryEntry entry)
{
// The route's first node runs single-threaded, so the queue exists before any parallel
// (Split/Multicast) section appends to it concurrently — hence a plain check-then-add here and
// a thread-safe ConcurrentQueue for the appends.
if (exchange.Properties.TryGetValue(PropertyKey, out var raw) && raw is ConcurrentQueue q)
{
q.Enqueue(entry);
return;
}
var created = new ConcurrentQueue();
created.Enqueue(entry);
exchange.Properties[PropertyKey] = created;
}
///
/// Renders the history as a table (columns: RouteId · Id · Processor · Elapsed/ms), the way Camel
/// prints it when an exchange fails. Returns an empty string when there is no history.
///
public static string Format(IExchange exchange)
{
var entries = GetEntries(exchange);
if (entries.Count == 0) return string.Empty;
var sb = new StringBuilder();
sb.AppendLine("Message History (routeId · id · processor · elapsed/ms):");
foreach (var e in entries)
sb.AppendLine($" {e.RouteId} · {e.NodeId} · {e.Label} · {e.ElapsedMs:F3}");
return sb.ToString().TrimEnd();
}
}