| | | 1 | | namespace Elsa.Diagnostics.ConsoleLogs.Services; |
| | | 2 | | |
| | | 3 | | internal static class ConsoleLogContextAccessorExtensions |
| | | 4 | | { |
| | | 5 | | public static IDisposable PushWorkflowExecution(this IConsoleLogContextAccessor accessor, WorkflowExecutionContext c |
| | | 6 | | { |
| | 6 | 7 | | var identity = context.Workflow.Identity; |
| | 6 | 8 | | return new CompositeScope( |
| | 6 | 9 | | accessor.PushMetadata(ConsoleLogMetadataKeys.WorkflowInstanceId, context.Id), |
| | 6 | 10 | | accessor.PushMetadata(ConsoleLogMetadataKeys.WorkflowDefinitionId, identity.DefinitionId), |
| | 6 | 11 | | accessor.PushMetadata(ConsoleLogMetadataKeys.WorkflowDefinitionVersionId, identity.Id)); |
| | | 12 | | } |
| | | 13 | | |
| | | 14 | | public static IDisposable PushActivityExecution(this IConsoleLogContextAccessor accessor, ActivityExecutionContext c |
| | | 15 | | { |
| | 3 | 16 | | return new CompositeScope( |
| | 3 | 17 | | accessor.PushWorkflowExecution(context.WorkflowExecutionContext), |
| | 3 | 18 | | PushMetadataIfNotEmpty(accessor, ConsoleLogMetadataKeys.ActivityInstanceId, context.Id), |
| | 3 | 19 | | PushMetadataIfNotEmpty(accessor, ConsoleLogMetadataKeys.ActivityId, context.Activity.Id), |
| | 3 | 20 | | PushMetadataIfNotEmpty(accessor, ConsoleLogMetadataKeys.ActivityNodeId, context.NodeId)); |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | private static IDisposable PushMetadataIfNotEmpty(IConsoleLogContextAccessor accessor, string key, string? value) => |
| | 9 | 24 | | string.IsNullOrWhiteSpace(value) ? EmptyScope.Instance : accessor.PushMetadata(key, value); |
| | | 25 | | |
| | | 26 | | private sealed class EmptyScope : IDisposable |
| | | 27 | | { |
| | 0 | 28 | | public static readonly EmptyScope Instance = new(); |
| | | 29 | | |
| | 0 | 30 | | private EmptyScope() |
| | | 31 | | { |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | public void Dispose() |
| | | 35 | | { |
| | 0 | 36 | | } |
| | | 37 | | } |
| | | 38 | | |
| | 9 | 39 | | private sealed class CompositeScope(params IDisposable[] scopes) : IDisposable |
| | | 40 | | { |
| | | 41 | | private bool _disposed; |
| | | 42 | | |
| | | 43 | | public void Dispose() |
| | | 44 | | { |
| | 9 | 45 | | if (_disposed) |
| | 0 | 46 | | return; |
| | | 47 | | |
| | 78 | 48 | | for (var i = scopes.Length - 1; i >= 0; i--) |
| | 30 | 49 | | scopes[i].Dispose(); |
| | | 50 | | |
| | 9 | 51 | | _disposed = true; |
| | 9 | 52 | | } |
| | | 53 | | } |
| | | 54 | | } |