using System; using System.Collections.Generic; using System.IO; using System.Linq; namespace MxGateway.Worker.Bootstrap; public sealed class WorkerConsoleLogger : IWorkerLogger { private readonly TextWriter _writer; public WorkerConsoleLogger(TextWriter writer) { _writer = writer ?? throw new ArgumentNullException(nameof(writer)); } public void Information(string eventName, IReadOnlyDictionary fields) { Write("Information", eventName, fields); } public void Error(string eventName, IReadOnlyDictionary fields) { Write("Error", eventName, fields); } private void Write( string level, string eventName, IReadOnlyDictionary fields) { Dictionary redactedFields = WorkerLogRedactor.RedactFields(fields); string fieldText = string.Join( " ", redactedFields.Select(field => $"{field.Key}={FormatValue(field.Value)}")); _writer.WriteLine($"level={level} event={eventName} {fieldText}".TrimEnd()); } private static string FormatValue(object? value) { return value?.ToString() ?? string.Empty; } }