Path: redb-route/redb.Route/Components/LogComponent.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/Components/LogComponent.cs ---- using System.Collections.Concurrent; using Microsoft.Extensions.Logging; using redb.Route.Abstractions; using redb.Route.Core; namespace redb.Route.Components; /// /// Log component. Scheme: "log". /// Logs the exchange body and headers to . /// URI: log://category?level=Information&showHeaders=true&showBody=true /// [EndpointRole(EndpointRole.Producer, Hint = "Use it as a To() destination.")] public class LogComponent : ComponentBase { private readonly ConcurrentDictionary _endpoints = new(StringComparer.OrdinalIgnoreCase); private readonly ILoggerFactory? _loggerFactory; /// Creates a log component with an optional logger factory. /// Logger factory for creating category-specific loggers. public LogComponent(ILoggerFactory? loggerFactory = null) { _loggerFactory = loggerFactory; } /// public override string Scheme => "log"; /// Gets the logger factory (may be null). internal ILoggerFactory? LoggerFactory => _loggerFactory; /// public override IEndpoint CreateEndpoint(EndpointUri uri) { ArgumentNullException.ThrowIfNull(uri); var options = new LogEndpointOptions(); options.BindFromUri(uri.RawParameters); options.Validate(); return _endpoints.GetOrAdd(uri.NormalizedKey, _ => new LogEndpoint(uri, this, options)); } } /// /// Options for log endpoints. /// public class LogEndpointOptions : EndpointOptions { /// /// Log level name (default: "Information"), any case: the .NET names Trace, Debug, Information, Warning, Error, /// Critical, None, or Apache Camel's TRACE, DEBUG, INFO, WARN, ERROR, OFF. Any other name is refused when the /// endpoint is created. /// public string Level { get; set; } = "Information"; private static readonly Dictionary LevelNames = new(StringComparer.OrdinalIgnoreCase) { ["Trace"] = LogLevel.Trace, ["Debug"] = LogLevel.Debug, ["Information"] = LogLevel.Information, ["Warning"] = LogLevel.Warning, ["Error"] = LogLevel.Error, ["Critical"] = LogLevel.Critical, ["None"] = LogLevel.None, // Apache Camel's LoggingLevel names; ERROR, DEBUG and TRACE are the .NET names already. ["INFO"] = LogLevel.Information, ["WARN"] = LogLevel.Warning, ["OFF"] = LogLevel.None, }; /// Whether to include headers in the log output (default: true). public bool ShowHeaders { get; set; } = true; /// Whether to include body in the log output (default: true). public bool ShowBody { get; set; } = true; /// Gets the parsed . internal LogLevel ParsedLevel => LevelNames[Level]; /// public override void Validate() { if (Level is null || !LevelNames.ContainsKey(Level)) throw new ArgumentException( $"log: level '{Level}' is not a log level. Use Trace, Debug, Information, Warning, Error, Critical or None, " + "or Apache Camel's TRACE, DEBUG, INFO, WARN, ERROR, OFF (any case).", nameof(Level)); } } /// /// Log endpoint. Produces a log entry for each exchange sent through it. /// public class LogEndpoint : EndpointBase { private readonly ILogger? _logger; private readonly LogEndpointOptions _logOptions; /// Creates a log endpoint. public LogEndpoint(EndpointUri uri, LogComponent component, LogEndpointOptions options) : base(uri, component, options) { _logOptions = options; var category = uri.Path; _logger = component.LoggerFactory?.CreateLogger( string.IsNullOrEmpty(category) ? "redb.Route.Log" : $"redb.Route.Log.{category}"); } /// Gets the configured logger (may be null). internal ILogger? Logger => _logger; /// Gets the log options. internal LogEndpointOptions LogOptions => _logOptions; /// public override IProducer CreateProducer() => new LogProducer(this); /// public override IConsumer CreateConsumer(IProcessor processor) { throw new NotSupportedException("Log endpoints do not support consumers. Use them only as To() destinations."); } } /// /// Log producer. Logs the exchange body and/or headers. /// public class LogProducer : IProducer { private readonly LogEndpoint _endpoint; private ILogger? _logger; /// Creates a log producer. public LogProducer(LogEndpoint endpoint) { _endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint)); _logger = (endpoint.Component as ComponentBase)?.Logger; } /// public Task Process(IExchange exchange, CancellationToken ct = default) { var logger = _endpoint.Logger; if (logger == null || !logger.IsEnabled(_endpoint.LogOptions.ParsedLevel)) return Task.CompletedTask; var options = _endpoint.LogOptions; var parts = new List(3); if (options.ShowBody) { var body = exchange.In.Body; parts.Add($"Body: {body ?? "(null)"}"); } if (options.ShowHeaders && exchange.In.Headers.Count > 0) { var headers = string.Join(", ", exchange.In.Headers.Select(h => $"{h.Key}={h.Value}")); parts.Add($"Headers: [{headers}]"); } var message = parts.Count > 0 ? string.Join(" | ", parts) : "(empty exchange)"; #pragma warning disable CA2254 // Template should be a static expression logger.Log(_endpoint.LogOptions.ParsedLevel, "Exchange [{RouteId}]: {Message}", exchange.RouteId ?? "(no-route)", message); #pragma warning restore CA2254 return Task.CompletedTask; } /// public Task Start(CancellationToken ct = default) { _logger?.LogInformation("Log producer started: {Name}", EndpointUri.ForLog(_endpoint.Uri.NormalizedKey, _logger)); return Task.CompletedTask; } /// public Task Stop(CancellationToken ct = default) { _logger?.LogInformation("Log producer stopped: {Name}", EndpointUri.ForLog(_endpoint.Uri.NormalizedKey, _logger)); return Task.CompletedTask; } }