mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2025-08-21 04:32:56 +00:00
Add logging to MessageBus initialization and error handling
This commit is contained in:
parent
1a1a385a0b
commit
4cf00f8123
@ -160,6 +160,7 @@ internal sealed class Program
|
|||||||
|
|
||||||
// Get the logging factory for e.g., static classes:
|
// Get the logging factory for e.g., static classes:
|
||||||
LOGGER_FACTORY = app.Services.GetRequiredService<ILoggerFactory>();
|
LOGGER_FACTORY = app.Services.GetRequiredService<ILoggerFactory>();
|
||||||
|
MessageBus.INSTANCE.Initialize(LOGGER_FACTORY.CreateLogger<MessageBus>());
|
||||||
|
|
||||||
// Get a program logger:
|
// Get a program logger:
|
||||||
var programLogger = app.Services.GetRequiredService<ILogger<Program>>();
|
var programLogger = app.Services.GetRequiredService<ILogger<Program>>();
|
||||||
|
@ -15,10 +15,18 @@ public sealed class MessageBus
|
|||||||
private readonly ConcurrentQueue<Message> messageQueue = new();
|
private readonly ConcurrentQueue<Message> messageQueue = new();
|
||||||
private readonly SemaphoreSlim sendingSemaphore = new(1, 1);
|
private readonly SemaphoreSlim sendingSemaphore = new(1, 1);
|
||||||
|
|
||||||
|
private static ILogger<MessageBus>? LOG;
|
||||||
|
|
||||||
private MessageBus()
|
private MessageBus()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Initialize(ILogger<MessageBus> logger)
|
||||||
|
{
|
||||||
|
LOG = logger;
|
||||||
|
LOG.LogInformation("Message bus initialized.");
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Define for which components and events you want to receive messages.
|
/// Define for which components and events you want to receive messages.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -61,11 +69,16 @@ public sealed class MessageBus
|
|||||||
|
|
||||||
var eventFilter = this.componentEvents[receiver];
|
var eventFilter = this.componentEvents[receiver];
|
||||||
if (eventFilter.Length == 0 || eventFilter.Contains(triggeredEvent))
|
if (eventFilter.Length == 0 || eventFilter.Contains(triggeredEvent))
|
||||||
|
|
||||||
// We don't await the task here because we don't want to block the message bus:
|
// We don't await the task here because we don't want to block the message bus:
|
||||||
_ = receiver.ProcessMessage(message.SendingComponent, message.TriggeredEvent, message.Data);
|
_ = receiver.ProcessMessage(message.SendingComponent, message.TriggeredEvent, message.Data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
LOG?.LogError(e, "Error while sending message.");
|
||||||
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
this.sendingSemaphore.Release();
|
this.sendingSemaphore.Release();
|
||||||
|
Loading…
Reference in New Issue
Block a user