| | | 1 | | namespace Elsa.Diagnostics.ConsoleLogs.Services; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Ambient console log context backed by the current async execution context. |
| | | 5 | | /// </summary> |
| | | 6 | | public sealed class ConsoleLogContextAccessor : IConsoleLogContextAccessor, ConsoleLogStreaming.Core.IConsoleLogMetadata |
| | | 7 | | { |
| | 1 | 8 | | private static readonly AsyncLocal<MetadataFrame?> CurrentFrame = new(); |
| | 1 | 9 | | private static readonly IReadOnlyDictionary<string, string> Empty = new Dictionary<string, string>(); |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Gets the process-wide Elsa console log context accessor. |
| | | 13 | | /// </summary> |
| | 19 | 14 | | public static ConsoleLogContextAccessor Instance { get; } = new(); |
| | | 15 | | |
| | 1 | 16 | | private ConsoleLogContextAccessor() |
| | | 17 | | { |
| | 1 | 18 | | } |
| | | 19 | | |
| | | 20 | | /// <inheritdoc /> |
| | | 21 | | public IReadOnlyDictionary<string, string> GetMetadata() |
| | | 22 | | { |
| | 46 | 23 | | var metadata = CurrentFrame.Value?.Metadata; |
| | 46 | 24 | | return metadata == null || metadata.Count == 0 |
| | 46 | 25 | | ? Empty |
| | 46 | 26 | | : new Dictionary<string, string>(metadata, StringComparer.OrdinalIgnoreCase); |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <inheritdoc /> |
| | | 30 | | public IDisposable PushMetadata(string key, string value) |
| | | 31 | | { |
| | 53 | 32 | | if (string.IsNullOrWhiteSpace(key)) |
| | 0 | 33 | | throw new ArgumentException("A metadata key is required.", nameof(key)); |
| | | 34 | | |
| | 53 | 35 | | if (string.IsNullOrWhiteSpace(value)) |
| | 0 | 36 | | throw new ArgumentException("A metadata value is required.", nameof(value)); |
| | | 37 | | |
| | 53 | 38 | | var previous = CurrentFrame.Value; |
| | 53 | 39 | | var metadata = previous?.Metadata != null |
| | 53 | 40 | | ? new Dictionary<string, string>(previous.Metadata, StringComparer.OrdinalIgnoreCase) |
| | 53 | 41 | | : new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| | | 42 | | |
| | 53 | 43 | | metadata[key] = value; |
| | 53 | 44 | | CurrentFrame.Value = new(metadata); |
| | 53 | 45 | | return new MetadataScope(previous); |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | | 49 | | public IDisposable PushWorkflowInstanceId(string workflowInstanceId) => |
| | 24 | 50 | | PushMetadata(ConsoleLogMetadataKeys.WorkflowInstanceId, workflowInstanceId); |
| | | 51 | | |
| | 130 | 52 | | private sealed record MetadataFrame(IReadOnlyDictionary<string, string> Metadata); |
| | | 53 | | |
| | 53 | 54 | | private sealed class MetadataScope(MetadataFrame? previous) : IDisposable |
| | | 55 | | { |
| | | 56 | | private bool _disposed; |
| | | 57 | | |
| | | 58 | | public void Dispose() |
| | | 59 | | { |
| | 53 | 60 | | if (_disposed) |
| | 0 | 61 | | return; |
| | | 62 | | |
| | 53 | 63 | | CurrentFrame.Value = previous; |
| | 53 | 64 | | _disposed = true; |
| | 53 | 65 | | } |
| | | 66 | | } |
| | | 67 | | } |