Skip to content

Commit

Permalink
LogMasked works for properties of any type (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
sungam3r authored Dec 20, 2024
1 parent 2675b66 commit b5864b2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
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 @@ 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 @@ -338,6 +345,25 @@ public void LogMaskedAttribute_Shows_First_NChars_Then_Replaces_All_With_Custom_
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();
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 @@ public void LogMaskedAttribute_Shows_First_NChars_And_Last_NChars_Replaces_Int_V


[Test]
public void LogMaskedAttribute_Nullify_Bool_Property()
public void LogMaskedAttribute_Mask_Bool_Property()
{
var customized = new CustomizedMaskedLogs2
{
Expand All @@ -836,7 +862,7 @@ public void LogMaskedAttribute_Nullify_Bool_Property()
props["Enabled1"].LiteralValue().ShouldBe(true);

props.ContainsKey("Enabled2").ShouldBeTrue();
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())),
};
}
}

0 comments on commit b5864b2

Please sign in to comment.