Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LogMasked works for properties of any type #141

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ var log = new LoggerConfiguration()

To prevent destructuring of a type or property at all, apply the `LogAsScalar` attribute.

## 6. Masking a string property
## 6. Masking a property

Apply the `LogMasked` attribute with various settings:

Expand All @@ -154,6 +154,7 @@ Apply the `LogMasked` attribute with various settings:
- **ShowLast:** Shows the last x characters in the property value.
- **PreserveLength:** If set, it will swap out each character with the default value. Note that this property will be ignored if Text has been set to custom value.

Masking works for all properties calling `ToString()` on their values.
Note that masking also works for properties of type `IEnumerable<string>` or derived from it, for example, `string[]` or `List<string>`.

### Examples
Expand Down Expand Up @@ -265,6 +266,12 @@ public class CustomizedMaskedLogs
[LogMasked(Text = "_REMOVED_", ShowFirst = 5)]
public Guid? ShowFirstFiveThenCustomMaskGuid { get; set; }

/// <summary>
/// Descending results in "Desce_REMOVED_"
/// </summary>
[LogMasked(Text = "_REMOVED_", ShowFirst = 5)]
public ListSortDirection ShowFirstFiveThenCustomMaskEnum { get; set; }

/// <summary>
/// 123456789 results in "123_REMOVED_"
/// </summary>
Expand Down Expand Up @@ -314,7 +321,7 @@ public class CustomizedMaskedLogs
public string? ShowFirstAndLastThreeAndCustomMaskInTheMiddlePreservedLengthIgnored { get; set; }
}
```
<sup><a href='/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs#L8-L163' title='Snippet source file'>snippet source</a> | <a href='#snippet-CustomizedMaskedLogs' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs#L9-L170' title='Snippet source file'>snippet source</a> | <a href='#snippet-CustomizedMaskedLogs' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## 7. Masking a string property with regular expressions
Expand Down
30 changes: 28 additions & 2 deletions src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.ComponentModel;
using Destructurama.Attributed.Tests.Support;
using NUnit.Framework;
using Serilog.Events;
Expand Down Expand Up @@ -111,6 +112,12 @@
[LogMasked(Text = "_REMOVED_", ShowFirst = 5)]
public Guid? ShowFirstFiveThenCustomMaskGuid { get; set; }

/// <summary>
/// Descending results in "Desce_REMOVED_"
/// </summary>
[LogMasked(Text = "_REMOVED_", ShowFirst = 5)]
public ListSortDirection ShowFirstFiveThenCustomMaskEnum { get; set; }

/// <summary>
/// 123456789 results in "123_REMOVED_"
/// </summary>
Expand Down Expand Up @@ -338,6 +345,25 @@
props["ShowFirstFiveThenCustomMaskGuid"].LiteralValue().ShouldBe("d3c4a_REMOVED_");
}

[Test]
public void LogMaskedAttribute_Shows_First_NChars_Then_Replaces_All_With_Custom_Mask_Enum()
{
// [LogMasked(Text = "_REMOVED_", ShowFirst = 5)]
// -> "Desce_REMOVED_"
var customized = new CustomizedMaskedLogs
{
ShowFirstFiveThenCustomMaskEnum = ListSortDirection.Descending,
};

var evt = DelegatingSink.Execute(customized);

var sv = (StructureValue)evt.Properties["Customized"];
var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value);

props.ContainsKey("ShowFirstFiveThenCustomMaskEnum").ShouldBeTrue();
Dismissed Show dismissed Hide dismissed
props["ShowFirstFiveThenCustomMaskEnum"].LiteralValue().ShouldBe("Desce_REMOVED_");
}

[Test]
public void LogMaskedAttribute_Shows_First_NChars_Then_Replaces_All_With_Custom_Mask_PreservedLength_Ignored()
{
Expand Down Expand Up @@ -819,7 +845,7 @@


[Test]
public void LogMaskedAttribute_Nullify_Bool_Property()
public void LogMaskedAttribute_Mask_Bool_Property()
{
var customized = new CustomizedMaskedLogs2
{
Expand All @@ -835,8 +861,8 @@
props.ContainsKey("Enabled1").ShouldBeTrue();
props["Enabled1"].LiteralValue().ShouldBe(true);

props.ContainsKey("Enabled2").ShouldBeTrue();

Check notice

Code scanning / CodeQL

Inefficient use of ContainsKey Note test

Inefficient use of 'ContainsKey' and
indexer
.
props["Enabled2"].LiteralValue().ShouldBeNull();
props["Enabled2"].LiteralValue().ShouldBe("***");
}

private class CustomizedMaskedLogs2
Expand Down
7 changes: 2 additions & 5 deletions src/Destructurama.Attributed/Attributed/LogMaskedAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,8 @@ private LogEventPropertyValue CreateValue(object? value)
return value switch
{
IEnumerable<string> strings => new SequenceValue(strings.Select(s => new ScalarValue(FormatMaskedValue(s)))),
string s => new ScalarValue(FormatMaskedValue(s)),
long l => new ScalarValue(FormatMaskedValue(l.ToString())),
int i => new ScalarValue(FormatMaskedValue(i.ToString())),
Guid g => new ScalarValue(FormatMaskedValue(g.ToString())),
_ => ScalarValue.Null,
null => ScalarValue.Null,
_ => new ScalarValue(FormatMaskedValue(value.ToString())),
};
}
}
Loading