| | | 1 | | using Elsa.Workflows.Models; |
| | | 2 | | using Elsa.Workflows.Pipelines.ActivityExecution; |
| | | 3 | | using JetBrains.Annotations; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Workflows.Middleware.Activities; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Adds extension methods to <see cref="ExecutionLogMiddleware"/>. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class ExecutionLogMiddlewareExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Installs the <see cref="ExecutionLogMiddleware"/> component in the activity execution pipeline. |
| | | 14 | | /// </summary> |
| | | 15 | | public static IActivityExecutionPipelineBuilder UseExecutionLogging(this IActivityExecutionPipelineBuilder pipelineB |
| | | 16 | | } |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// An activity execution middleware component that extracts execution details as <see cref="WorkflowExecutionLogEntry"/ |
| | | 20 | | /// </summary> |
| | | 21 | | [UsedImplicitly] |
| | 421 | 22 | | public class ExecutionLogMiddleware(ActivityMiddlewareDelegate next) : IActivityExecutionMiddleware |
| | | 23 | | { |
| | | 24 | | /// <inheritdoc /> |
| | | 25 | | public async ValueTask InvokeAsync(ActivityExecutionContext context) |
| | | 26 | | { |
| | 3019 | 27 | | context.AddExecutionLogEntry(IsActivityBookmarked(context) ? "Resumed" : "Started"); |
| | | 28 | | |
| | | 29 | | try |
| | | 30 | | { |
| | 3019 | 31 | | await next(context); |
| | | 32 | | |
| | 3006 | 33 | | if (context.Status == ActivityStatus.Running) |
| | | 34 | | { |
| | 2068 | 35 | | if (IsActivityBookmarked(context)) |
| | 45 | 36 | | context.AddExecutionLogEntry("Suspended"); |
| | | 37 | | } |
| | 3006 | 38 | | } |
| | 13 | 39 | | catch (Exception exception) |
| | | 40 | | { |
| | 13 | 41 | | context.AddExecutionLogEntry("Faulted", |
| | 13 | 42 | | message: exception.Message, |
| | 13 | 43 | | payload: new |
| | 13 | 44 | | { |
| | 13 | 45 | | Exception = exception.GetType().FullName, |
| | 13 | 46 | | exception.Message, |
| | 13 | 47 | | exception.Source, |
| | 13 | 48 | | exception.Data, |
| | 13 | 49 | | exception.StackTrace, |
| | 13 | 50 | | InnerException = exception.InnerException?.GetType().FullName, |
| | 13 | 51 | | }); |
| | | 52 | | |
| | 13 | 53 | | throw; |
| | | 54 | | } |
| | 3006 | 55 | | } |
| | | 56 | | |
| | | 57 | | private static bool IsActivityBookmarked(ActivityExecutionContext context) |
| | | 58 | | { |
| | 5177 | 59 | | return context.WorkflowExecutionContext.Bookmarks.Any(b => b.ActivityNodeId.Equals(context.ActivityNode.NodeId)) |
| | | 60 | | } |
| | | 61 | | } |