coravel icon indicating copy to clipboard operation
coravel copied to clipboard

Feat: Log Unhandled Exception During Queue

Open ryanelian opened this issue 4 years ago • 2 comments

Describe the solution you'd like Currently, a global Queue error logging requires some custom glue code:

            var queueLogger = serviceProvider.GetRequiredService<ILogger<IQueue>>();
            serviceProvider.ConfigureQueue()
                .OnError(ex =>
                {
                    queueLogger.LogError(ex, "An unhandled exception has occurred when processing a queued background task.");
                })
                .LogQueuedTaskProgress(queueLogger);

I wish that LogQueuedTaskProgress does error logging automatically as well.

Or better yet, by default log all unhandled exceptions without requiring user to configure that. (Since most if not all ASP.NET Core apps are configured with Microsoft.Extensions.Logging.ILogger service out of the box)

Describe alternatives you've considered

  • Writing an extension method .OnErrorLogToDefaultLogger

ryanelian avatar Feb 27 '20 22:02 ryanelian

Would agree with this 👍

jamesmh avatar Feb 28 '20 13:02 jamesmh

Oh one more thing.

Currently the glue code cannot report WHICH Event / Event Listener causing the error. This poses an enormously challenging debugging problem.

2020-02-27 19:04:48.942 +07:00 [INF] Queued task started...
2020-02-27 19:04:48.944 +07:00 [ERR] An unhandled exception has occurred when processing a queued background task.
System.Reflection.TargetException: Non-static method requires a target.
   at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
   at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at Coravel.Events.Dispatcher.Broadcast[TEvent](TEvent toBroadcast)
   at Coravel.Queuing.Queue.<>c__DisplayClass12_0`1.<<QueueBroadcast>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Coravel.Tasks.ActionOrAsyncFunc.Invoke()
   at Coravel.Queuing.Queue.InvokeTask(ActionOrAsyncFunc task)
2020-02-27 19:04:48.942 +07:00 [INF] Queued task started...
2020-02-27 19:04:48.944 +07:00 [ERR] An unhandled exception has occurred when processing a queued background task.
System.Reflection.TargetException: Non-static method requires a target.
   at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
   at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at Coravel.Events.Dispatcher.Broadcast[TEvent](TEvent toBroadcast)
   at Coravel.Queuing.Queue.<>c__DisplayClass12_0`1.<<QueueBroadcast>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Coravel.Tasks.ActionOrAsyncFunc.Invoke()
   at Coravel.Queuing.Queue.InvokeTask(ActionOrAsyncFunc task)

I wish the unhandled exception logger reports which Event Listener / Invocable causing the error.

ryanelian avatar Feb 29 '20 01:02 ryanelian