Skip to content

Commit ff431c2

Browse files
committed
Refactor event handling and improve logging
Introduce `LogEventArgs` for `LogCreated` and `LogCreating` events in `IAutomationClient.cs` and `AutomationClient.cs`. Update `using` directives to include `G4.Models.Events`. Modify `RegisterInvokerEvents` to accept `G4AutomationModel automation` parameter. Update event handler registration to use `LogEventArgs` with properties like `Automation`, `AutomationGroup`, `Invoker`, and `LogMessage`. Use named arguments in `NewDelegate` method. Add error handling and logging to ensure exceptions are properly logged.
1 parent 9319a24 commit ff431c2

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

src/G4.Api/Abstractions/IAutomationClient.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using G4.Api.Models;
22
using G4.Models;
3+
using G4.Models.Events;
34
using G4.Plugins;
45

56
using Microsoft.Extensions.Logging;
@@ -58,12 +59,12 @@ public interface IAutomationClient
5859
/// <summary>
5960
/// Occurs after a log entry has been created.
6061
/// </summary>
61-
event EventHandler<IDictionary<string, object>> LogCreated;
62+
event EventHandler<LogEventArgs> LogCreated;
6263

6364
/// <summary>
6465
/// Occurs before a log entry is created.
6566
/// </summary>
66-
event EventHandler<IDictionary<string, object>> LogCreating;
67+
event EventHandler<LogEventArgs> LogCreating;
6768

6869
/// <summary>
6970
/// Occurs when an error occurs during action execution.

src/G4.Api/Clients/AutomationClient.cs

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using G4.Cache;
55
using G4.Extensions;
66
using G4.Models;
7+
using G4.Models.Events;
78
using G4.Plugins;
89
using G4.Plugins.Engine;
910

@@ -50,10 +51,10 @@ internal class AutomationClient(ILogger logger) : ClientBase, IAutomationClient
5051
public event EventHandler<JobEventArgs> JobStatusChanged;
5152

5253
/// <inheritdoc />
53-
public event EventHandler<IDictionary<string, object>> LogCreated;
54+
public event EventHandler<LogEventArgs> LogCreated;
5455

5556
/// <inheritdoc />
56-
public event EventHandler<IDictionary<string, object>> LogCreating;
57+
public event EventHandler<LogEventArgs> LogCreating;
5758

5859
/// <inheritdoc />
5960
public event EventHandler<(RuleEventArgs EventArguments, Exception Exception)> OnRuleError;
@@ -229,7 +230,7 @@ private static ConcurrentBag<AutomationQueueModel> NewAutomationRequests(
229230
// Set up new invoker events using the client and the newly created queue model
230231
if (registerStatusEvents)
231232
{
232-
RegisterInvokerEvents(client, invoker);
233+
RegisterInvokerEvents(client, automation, invoker);
233234
}
234235

235236
// Invoke the AutomationRequestInitialized event on the client
@@ -244,7 +245,7 @@ private static ConcurrentBag<AutomationQueueModel> NewAutomationRequests(
244245
}
245246

246247
// Registers event handlers for the invoker by wrapping the client's event handlers with exception handling and logging.
247-
private static void RegisterInvokerEvents(AutomationClient client, AutomationInvoker invoker)
248+
private static void RegisterInvokerEvents(AutomationClient client, G4AutomationModel automation, AutomationInvoker invoker)
248249
{
249250
// Creates a new delegate that wraps an event handler with exception handling and logging.
250251
static EventHandler<T> NewDelegate<T>(AutomationClient client, EventHandler<T> eventHandler)
@@ -255,7 +256,7 @@ static EventHandler<T> NewDelegate<T>(AutomationClient client, EventHandler<T> e
255256
try
256257
{
257258
// Safely invoke the event handler if it's not null
258-
eventHandler?.Invoke(sender, args);
259+
eventHandler?.Invoke(sender, e: args);
259260
}
260261
catch (Exception e)
261262
{
@@ -287,8 +288,51 @@ static EventHandler<T> NewDelegate<T>(AutomationClient client, EventHandler<T> e
287288
invoker.StageInvoking += NewDelegate(client, client.StageInvoking);
288289
invoker.StageStatusChanged += NewDelegate(client, client.StageStatusChanged);
289290

290-
logger.LogCreated += NewDelegate(client, client.LogCreated);
291-
logger.LogCreating += NewDelegate(client, client.LogCreating);
291+
logger.LogCreated += (sender, args) =>
292+
{
293+
try
294+
{
295+
// Safely invoke the event handler if it's not null
296+
client.LogCreated?.Invoke(sender, e: new LogEventArgs
297+
{
298+
Automation = automation.Reference?.Id,
299+
AutomationGroup = automation.Reference?.GroupId,
300+
Invoker = invoker.Reference,
301+
LogMessage = args
302+
});
303+
}
304+
catch (Exception e)
305+
{
306+
// Log the error if an exception occurs during event invocation
307+
client.Logger.LogError(
308+
exception: e,
309+
message: "Error in event handler. Sender: {Sender}, EventArgs: {EventArgs}",
310+
sender?.GetType().FullName, typeof(object).Name);
311+
}
312+
};
313+
314+
logger.LogCreating += (sender, args) =>
315+
{
316+
try
317+
{
318+
// Safely invoke the event handler if it's not null
319+
client.LogCreating?.Invoke(sender, e: new LogEventArgs
320+
{
321+
Automation = automation.Reference?.Id,
322+
AutomationGroup = automation.GroupId,
323+
Invoker = invoker.Reference,
324+
LogMessage = args
325+
});
326+
}
327+
catch (Exception e)
328+
{
329+
// Log the error if an exception occurs during event invocation
330+
client.Logger.LogError(
331+
exception: e,
332+
message: "Error in event handler. Sender: {Sender}, EventArgs: {EventArgs}",
333+
sender?.GetType().FullName, typeof(object).Name);
334+
}
335+
};
292336
}
293337
#endregion
294338
}

0 commit comments

Comments
 (0)