| | | 1 | | using CShells.AspNetCore.Features; |
| | | 2 | | using CShells.FastEndpoints.Features; |
| | | 3 | | using CShells.Features; |
| | | 4 | | using Elsa.Diagnostics.StructuredLogs.Extensions; |
| | | 5 | | using Elsa.Diagnostics.StructuredLogs.Options; |
| | | 6 | | using Elsa.PackageManifest.Generator.Hints; |
| | | 7 | | using JetBrains.Annotations; |
| | | 8 | | using Microsoft.AspNetCore.Routing; |
| | | 9 | | using Microsoft.Extensions.DependencyInjection; |
| | | 10 | | using Microsoft.Extensions.Hosting; |
| | | 11 | | |
| | | 12 | | namespace Elsa.Diagnostics.StructuredLogs.ShellFeatures; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Provides live structured log streaming over REST and SignalR. |
| | | 16 | | /// </summary> |
| | | 17 | | [ShellFeature( |
| | | 18 | | DisplayName = "Structured Logs", |
| | | 19 | | Description = "Provides live structured log streaming over REST and SignalR", |
| | | 20 | | DependsOn = ["ElsaFastEndpoints"])] |
| | | 21 | | [UsedImplicitly] |
| | | 22 | | public class StructuredLogsFeature : IFastEndpointsShellFeature, IWebShellFeature |
| | | 23 | | { |
| | | 24 | | private const string SensitiveNamesDefaultValue = "authorization, token, password, secret, api-key, apikey, cookie, |
| | | 25 | | private const string SensitiveTextPatternsDefaultValue = "(?i)bearer\\s+[A-Za-z0-9._~+/=-]+; (?i)(password|secret|to |
| | 1 | 26 | | private static readonly StructuredLogsOptions DefaultOptions = new(); |
| | | 27 | | |
| | | 28 | | [ManifestSetting( |
| | | 29 | | DisplayName = "Recent Log Capacity", |
| | | 30 | | Description = "Maximum number of recent log events retained in memory.", |
| | | 31 | | Category = "Diagnostics", |
| | | 32 | | DefaultValue = "5000", |
| | | 33 | | RestartRequired = true)] |
| | 4 | 34 | | public int RecentLogCapacity { get; set; } = DefaultOptions.RecentLogCapacity; |
| | | 35 | | |
| | | 36 | | [ManifestSetting( |
| | | 37 | | DisplayName = "Subscriber Channel Capacity", |
| | | 38 | | Description = "Maximum number of queued log events per streaming subscriber.", |
| | | 39 | | Category = "Diagnostics", |
| | | 40 | | DefaultValue = "1000", |
| | | 41 | | RestartRequired = true)] |
| | 4 | 42 | | public int SubscriberChannelCapacity { get; set; } = DefaultOptions.SubscriberChannelCapacity; |
| | | 43 | | |
| | | 44 | | [ManifestSetting( |
| | | 45 | | DisplayName = "Maximum Recent Log Query Size", |
| | | 46 | | Description = "Maximum number of recent log events returned by a query.", |
| | | 47 | | Category = "Diagnostics", |
| | | 48 | | DefaultValue = "1000", |
| | | 49 | | RestartRequired = true)] |
| | 4 | 50 | | public int MaxRecentLogQuerySize { get; set; } = DefaultOptions.MaxRecentLogQuerySize; |
| | | 51 | | |
| | | 52 | | [ManifestSetting( |
| | | 53 | | DisplayName = "Source Heartbeat Timeout", |
| | | 54 | | Description = "Time after which a structured log source is considered inactive.", |
| | | 55 | | Category = "Diagnostics", |
| | | 56 | | DefaultValue = "00:00:30", |
| | | 57 | | RestartRequired = true)] |
| | 4 | 58 | | public TimeSpan SourceHeartbeatTimeout { get; set; } = DefaultOptions.SourceHeartbeatTimeout; |
| | | 59 | | |
| | | 60 | | [ManifestSetting( |
| | | 61 | | DisplayName = "Include Internal Structured Logs", |
| | | 62 | | Description = "Include log events produced by the structured logs subsystem itself.", |
| | | 63 | | Category = "Diagnostics", |
| | | 64 | | DefaultValue = "false", |
| | | 65 | | Advanced = true, |
| | | 66 | | RestartRequired = true)] |
| | 4 | 67 | | public bool IncludeStructuredLogsInternalLogs { get; set; } = DefaultOptions.IncludeStructuredLogsInternalLogs; |
| | | 68 | | |
| | | 69 | | [ManifestSetting( |
| | | 70 | | DisplayName = "Sensitive Names", |
| | | 71 | | Description = "Property names whose values should be redacted from structured log events.", |
| | | 72 | | Category = "Redaction", |
| | | 73 | | DefaultValue = SensitiveNamesDefaultValue, |
| | | 74 | | RestartRequired = true)] |
| | 4 | 75 | | public ICollection<string> SensitiveNames { get; set; } = [..DefaultOptions.SensitiveNames]; |
| | | 76 | | |
| | | 77 | | [ManifestSetting( |
| | | 78 | | DisplayName = "Sensitive Text Patterns", |
| | | 79 | | Description = "Text patterns whose values should be redacted from structured log events.", |
| | | 80 | | Category = "Redaction", |
| | | 81 | | DefaultValue = SensitiveTextPatternsDefaultValue, |
| | | 82 | | Advanced = true, |
| | | 83 | | RestartRequired = true)] |
| | 4 | 84 | | public ICollection<string> SensitiveTextPatterns { get; set; } = [..DefaultOptions.SensitiveTextPatterns]; |
| | | 85 | | |
| | | 86 | | public void ConfigureServices(IServiceCollection services) |
| | | 87 | | { |
| | 1 | 88 | | services.AddStructuredLogsServices(ConfigureOptions); |
| | 1 | 89 | | } |
| | | 90 | | |
| | | 91 | | public void MapEndpoints(IEndpointRouteBuilder endpoints, IHostEnvironment? environment) |
| | | 92 | | { |
| | 0 | 93 | | endpoints.MapStructuredLogsHub(); |
| | 0 | 94 | | } |
| | | 95 | | |
| | | 96 | | private void ConfigureOptions(StructuredLogsOptions options) |
| | | 97 | | { |
| | 1 | 98 | | options.RecentLogCapacity = RecentLogCapacity; |
| | 1 | 99 | | options.SubscriberChannelCapacity = SubscriberChannelCapacity; |
| | 1 | 100 | | options.MaxRecentLogQuerySize = MaxRecentLogQuerySize; |
| | 1 | 101 | | options.SourceHeartbeatTimeout = SourceHeartbeatTimeout; |
| | 1 | 102 | | options.IncludeStructuredLogsInternalLogs = IncludeStructuredLogsInternalLogs; |
| | 1 | 103 | | options.SensitiveNames = [..SensitiveNames]; |
| | 1 | 104 | | options.SensitiveTextPatterns = [..SensitiveTextPatterns]; |
| | 1 | 105 | | } |
| | | 106 | | } |