using PCC.Common.EventBus; namespace PCC.Common.Logging; public class LoggingIntoEventBus : ILoggerProvider { public IEventBus? EventBus { get; set; } public void Dispose() { } public ILogger CreateLogger(string categoryName) { return new LoggingIntoEventBusLogger(categoryName, this); } private class LoggingIntoEventBusLogger(string categoryName, LoggingIntoEventBus owner) : ILogger { public IDisposable? BeginScope(TState state) where TState : notnull => default; public bool IsEnabled(LogLevel logLevel) => true; public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) { if (owner.EventBus == null) return; var content = Convert.ToString(state); if (exception != null) content = $"{content} {exception}"; var formatted = $"[{DateTime.Now:dd HH:mm:ss}] {categoryName} {content}"; owner.EventBus.Publish(new LoggingEvent(logLevel, categoryName, formatted)); } } }