< Summary

Information
Class: Elsa.Diagnostics.StructuredLogs.Services.StructuredLogRedactor
Assembly: Elsa.Diagnostics.StructuredLogs
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Diagnostics.StructuredLogs/Services/StructuredLogRedactor.cs
Line coverage
100%
Covered lines: 27
Uncovered lines: 0
Coverable lines: 27
Total lines: 58
Line coverage: 100%
Branch coverage
80%
Covered branches: 8
Total branches: 10
Branch coverage: 80%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Redact(...)50%22100%
RedactException(...)75%44100%
RedactDictionary(...)100%11100%
RedactValue(...)100%44100%
IsSensitiveName(...)100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Diagnostics.StructuredLogs/Services/StructuredLogRedactor.cs

#LineLine coverage
 1using Elsa.Diagnostics.StructuredLogs.Contracts;
 2using Elsa.Diagnostics.StructuredLogs.Models;
 3using Elsa.Diagnostics.StructuredLogs.Options;
 4using Microsoft.Extensions.Options;
 5using System.Text.RegularExpressions;
 6
 7namespace Elsa.Diagnostics.StructuredLogs.Services;
 8
 89public class StructuredLogRedactor(IOptions<StructuredLogsOptions> options) : IStructuredLogRedactor
 10{
 11    private const string Redacted = "[Redacted]";
 812    private readonly HashSet<string> _sensitiveNames = options.Value.SensitiveNames.ToHashSet(StringComparer.OrdinalIgno
 813    private readonly IReadOnlyCollection<Regex> _sensitiveTextPatterns = options.Value.SensitiveTextPatterns
 2414        .Select(pattern => new Regex(pattern, RegexOptions.Compiled | RegexOptions.CultureInvariant))
 815        .ToList();
 16
 17    public StructuredLogEvent Redact(StructuredLogEvent logEvent)
 18    {
 719        return logEvent with
 720        {
 721            Message = RedactValue("message", logEvent.Message) ?? string.Empty,
 722            MessageTemplate = RedactValue("messageTemplate", logEvent.MessageTemplate),
 723            Exception = RedactException(logEvent.Exception),
 724            Scopes = RedactDictionary(logEvent.Scopes),
 725            Properties = RedactDictionary(logEvent.Properties)
 726        };
 27    }
 28
 29    private StructuredLogException? RedactException(StructuredLogException? exception)
 30    {
 731        if (exception == null)
 532            return null;
 33
 234        return exception with
 235        {
 236            Message = RedactValue("exceptionMessage", exception.Message) ?? string.Empty,
 237            StackTrace = RedactValue("stackTrace", exception.StackTrace)
 238        };
 39    }
 40
 41    private Dictionary<string, string?> RedactDictionary(IDictionary<string, string?> values)
 42    {
 2843        return values.ToDictionary(x => x.Key, x => RedactValue(x.Key, x.Value), StringComparer.OrdinalIgnoreCase);
 44    }
 45
 46    private string? RedactValue(string name, string? value)
 47    {
 2548        if (value == null)
 549            return null;
 50
 2051        if (IsSensitiveName(name))
 352            return Redacted;
 53
 6854        return _sensitiveTextPatterns.Aggregate(value, (current, pattern) => pattern.Replace(current, Redacted));
 55    }
 56
 18057    private bool IsSensitiveName(string name) => _sensitiveNames.Any(sensitiveName => name.Contains(sensitiveName, Strin
 58}