4
4
using G4 . Cache ;
5
5
using G4 . Extensions ;
6
6
using G4 . Models ;
7
+ using G4 . Models . Events ;
7
8
using G4 . Plugins ;
8
9
using G4 . Plugins . Engine ;
9
10
@@ -50,10 +51,10 @@ internal class AutomationClient(ILogger logger) : ClientBase, IAutomationClient
50
51
public event EventHandler < JobEventArgs > JobStatusChanged ;
51
52
52
53
/// <inheritdoc />
53
- public event EventHandler < IDictionary < string , object > > LogCreated ;
54
+ public event EventHandler < LogEventArgs > LogCreated ;
54
55
55
56
/// <inheritdoc />
56
- public event EventHandler < IDictionary < string , object > > LogCreating ;
57
+ public event EventHandler < LogEventArgs > LogCreating ;
57
58
58
59
/// <inheritdoc />
59
60
public event EventHandler < ( RuleEventArgs EventArguments , Exception Exception ) > OnRuleError ;
@@ -229,7 +230,7 @@ private static ConcurrentBag<AutomationQueueModel> NewAutomationRequests(
229
230
// Set up new invoker events using the client and the newly created queue model
230
231
if ( registerStatusEvents )
231
232
{
232
- RegisterInvokerEvents ( client , invoker ) ;
233
+ RegisterInvokerEvents ( client , automation , invoker ) ;
233
234
}
234
235
235
236
// Invoke the AutomationRequestInitialized event on the client
@@ -244,7 +245,7 @@ private static ConcurrentBag<AutomationQueueModel> NewAutomationRequests(
244
245
}
245
246
246
247
// 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 )
248
249
{
249
250
// Creates a new delegate that wraps an event handler with exception handling and logging.
250
251
static EventHandler < T > NewDelegate < T > ( AutomationClient client , EventHandler < T > eventHandler )
@@ -255,7 +256,7 @@ static EventHandler<T> NewDelegate<T>(AutomationClient client, EventHandler<T> e
255
256
try
256
257
{
257
258
// Safely invoke the event handler if it's not null
258
- eventHandler ? . Invoke ( sender , args ) ;
259
+ eventHandler ? . Invoke ( sender , e : args ) ;
259
260
}
260
261
catch ( Exception e )
261
262
{
@@ -287,8 +288,51 @@ static EventHandler<T> NewDelegate<T>(AutomationClient client, EventHandler<T> e
287
288
invoker . StageInvoking += NewDelegate ( client , client . StageInvoking ) ;
288
289
invoker . StageStatusChanged += NewDelegate ( client , client . StageStatusChanged ) ;
289
290
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
+ } ;
292
336
}
293
337
#endregion
294
338
}
0 commit comments