diff --git a/README.md b/README.md index 7e4cc15..d070a9d 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ public class PersonalData public string? Name { get; set; } } ``` -snippet source | anchor +snippet source | anchor ## 2. Ignoring a property @@ -278,7 +278,7 @@ public class CustomizedMaskedLogs public string? ShowFirstAndLastThreeAndCustomMaskInTheMiddlePreservedLengthIgnored { get; set; } } ``` -snippet source | anchor +snippet source | anchor ## 7. Masking a string property with regular expressions diff --git a/src/Destructurama.Attributed.Tests/AttributedDestructuringTests.cs b/src/Destructurama.Attributed.Tests/AttributedDestructuringTests.cs index 832a687..7b3665c 100644 --- a/src/Destructurama.Attributed.Tests/AttributedDestructuringTests.cs +++ b/src/Destructurama.Attributed.Tests/AttributedDestructuringTests.cs @@ -1,6 +1,5 @@ using Destructurama.Attributed.Tests.Support; using NUnit.Framework; -using Serilog; using Serilog.Events; using Shouldly; @@ -13,16 +12,7 @@ public class AttributedDestructuringTests public void Throwing_Accessor_Should_Be_Handled() { // Setup - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var obj = new ClassWithThrowingAccessor(); - - // Execute - log.Information("Here is {@Customized}", obj); + var evt = DelegatingSink.Execute(new ClassWithThrowingAccessor()); // Verify var sv = (StructureValue)evt.Properties["Customized"]; @@ -34,13 +24,6 @@ public void Throwing_Accessor_Should_Be_Handled() [Test] public void AttributesAreConsultedWhenDestructuring() { - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new Customized { ImmutableScalar = new(), @@ -55,7 +38,7 @@ public void AttributesAreConsultedWhenDestructuring() } }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); diff --git a/src/Destructurama.Attributed.Tests/IgnoreNullPropertiesTests.cs b/src/Destructurama.Attributed.Tests/IgnoreNullPropertiesTests.cs index 7db1e49..4762073 100644 --- a/src/Destructurama.Attributed.Tests/IgnoreNullPropertiesTests.cs +++ b/src/Destructurama.Attributed.Tests/IgnoreNullPropertiesTests.cs @@ -1,7 +1,6 @@ using System.Collections; using Destructurama.Attributed.Tests.Support; using NUnit.Framework; -using Serilog; using Serilog.Events; using Shouldly; @@ -111,8 +110,8 @@ IEnumerator IEnumerable.GetEnumerator() } /// - /// At least one attribute from Destructurma.Attributed is enough to ignore all default properties on IEnumerable, - /// when IgnoreNullProperties is true. + /// At least one attribute from Destructurma.Attributed is enough to ignore all + /// default properties on IEnumerable, when IgnoreNullProperties is true. /// private class CustomEnumerableAttributed : IEnumerable { @@ -130,8 +129,7 @@ public IEnumerator GetEnumerator() yield return 1; } - IEnumerator IEnumerable.GetEnumerator() - => GetEnumerator(); + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } [SetUp] @@ -149,16 +147,9 @@ public void TearDown() [Test] public void NotLoggedIfNull_Uninitialized() { - LogEvent? evt = null; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes(x => x.IgnoreNullProperties = true) - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new NotLoggedIfNull(); - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized, configure: x => x.IgnoreNullProperties = true); var sv = (StructureValue)evt!.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -179,13 +170,6 @@ public void NotLoggedIfNull_Uninitialized() [Test] public void NotLoggedIfNull_Initialized() { - LogEvent? evt = null; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes(x => x.IgnoreNullProperties = true) - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var dateTime = DateTime.UtcNow; var theStruct = new NotLoggedIfNullStruct { @@ -219,7 +203,7 @@ public void NotLoggedIfNull_Initialized() StructPartiallyInitialized = theStructPartiallyUnitialized, }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized, configure: x => x.IgnoreNullProperties = true); var sv = (StructureValue)evt!.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -275,16 +259,9 @@ public void NotLoggedIfNull_Initialized() [Test] public void WithMask_NotLoggedIfNull_Uninitialized() { - LogEvent? evt = null; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes(x => x.IgnoreNullProperties = true) - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new AttributedWithMask(); - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized, configure: x => x.IgnoreNullProperties = true); var sv = (StructureValue)evt!.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -296,20 +273,13 @@ public void WithMask_NotLoggedIfNull_Uninitialized() [Test] public void WithMask_NotLoggedIfNull_Initialized() { - LogEvent? evt = null; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes(x => x.IgnoreNullProperties = true) - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new AttributedWithMask { String = "Foo[Masked]", Object = "Bar[Masked]", }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized, configure: x => x.IgnoreNullProperties = true); var sv = (StructureValue)evt!.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -324,13 +294,6 @@ public void WithMask_NotLoggedIfNull_Initialized() [Test] public void EnumerableIgnored() { - LogEvent? evt = null; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes(x => x.IgnoreNullProperties = true) - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomEnumerableDestructionIgnored() { Integer = 0, @@ -340,7 +303,7 @@ public void EnumerableIgnored() } }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized, configure: x => x.IgnoreNullProperties = true); var sv = evt!.Properties["Customized"]; sv.ShouldBeOfType(); @@ -349,13 +312,6 @@ public void EnumerableIgnored() [Test] public void EnumerableDestructedAsStruct() { - LogEvent? evt = null; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes(x => x.IgnoreNullProperties = true) - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomEnumerableAttributed { Integer = 0, @@ -366,7 +322,7 @@ public void EnumerableDestructedAsStruct() }, }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized, configure: x => x.IgnoreNullProperties = true); var sv = (StructureValue)evt!.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -384,16 +340,9 @@ public void EnumerableDestructedAsStruct() [Test] public void NotLoggedIfNullAttribute_Uninitialized() { - LogEvent? evt = null; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes(x => x.IgnoreNullProperties = false) - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new NotLoggedIfNullAttributed(); - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized, configure: x => x.IgnoreNullProperties = true); var sv = (StructureValue)evt!.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -414,13 +363,6 @@ public void NotLoggedIfNullAttribute_Uninitialized() [Test] public void NotLoggedIfNullAttribute_Initialized() { - LogEvent? evt = null; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes(x => x.IgnoreNullProperties = false) - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var dateTime = DateTime.UtcNow; var theStruct = new NotLoggedIfNullStruct { @@ -454,7 +396,7 @@ public void NotLoggedIfNullAttribute_Initialized() StructPartiallyInitialized = theStructPartiallyUnitialized, }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized, configure: x => x.IgnoreNullProperties = false); var sv = (StructureValue)evt!.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); diff --git a/src/Destructurama.Attributed.Tests/LogWithNameAttributedTests.cs b/src/Destructurama.Attributed.Tests/LogWithNameAttributeTests.cs similarity index 63% rename from src/Destructurama.Attributed.Tests/LogWithNameAttributedTests.cs rename to src/Destructurama.Attributed.Tests/LogWithNameAttributeTests.cs index b334770..1f25c14 100644 --- a/src/Destructurama.Attributed.Tests/LogWithNameAttributedTests.cs +++ b/src/Destructurama.Attributed.Tests/LogWithNameAttributeTests.cs @@ -1,32 +1,24 @@ using Destructurama.Attributed.Tests.Support; using NUnit.Framework; -using Serilog; using Serilog.Events; using Shouldly; namespace Destructurama.Attributed.Tests; [TestFixture] -public class LogWithNameAttributedTests +public class LogWithNameAttributeTests { [Test] public void AttributesAreConsultedWhenDestructuring() { - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var personalData = new PersonalData { Name = "John Doe" }; - log.Information("Here is {@PersonData}", personalData); + var evt = DelegatingSink.Execute(personalData); - var sv = (StructureValue)evt.Properties["PersonData"]; + var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); var literalValue = props["FullName"].LiteralValue(); diff --git a/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs b/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs index fc3b910..567ee1d 100644 --- a/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs +++ b/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs @@ -1,6 +1,5 @@ using Destructurama.Attributed.Tests.Support; using NUnit.Framework; -using Serilog; using Serilog.Events; using Shouldly; @@ -135,20 +134,12 @@ public void LogMaskedAttribute_Replaces_Value_With_DefaultStars_Mask() { // [LogMasked] // 123456789 -> "***" - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedMaskedLogs { DefaultMasked = "123456789" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -162,20 +153,12 @@ public void LogMaskedAttribute_Replaces_Array_Value_With_DefaultStars_Mask() { // [LogMasked] // [123456789,123456789,123456789] results in [***,***,***] - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedMaskedLogs { DefaultMaskedArray = ["123456789", "123456789", "123456789"] }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -191,20 +174,12 @@ public void LogMaskedAttribute_Replaces_Value_With_DefaultStars_Mask_And_Preserv { // [LogMasked] // 123456789 -> "*********" - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedMaskedLogs { DefaultMaskedPreserved = "123456789" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -218,20 +193,12 @@ public void LogMaskedAttribute_Replaces_Value_With_DefaultStars_Mask_And_Not_Pre { // [LogMasked] // "" -> "***" - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedMaskedLogs { DefaultMaskedNotPreservedOnEmptyString = "" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -245,20 +212,12 @@ public void LogMaskedAttribute_Replaces_Value_With_Provided_Mask() { // [LogMasked(Text = "#")] // 123456789 -> "_REMOVED_" - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedMaskedLogs { CustomMasked = "123456789" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -272,20 +231,12 @@ public void LogMaskedAttribute_Replaces_Value_With_Provided_Empty_Mask() { // [LogMasked(Text = "#")] // 123456789 -> "_REMOVED_" - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedMaskedLogs { CustomMaskedWithEmptyString = "123456789" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -299,20 +250,12 @@ public void LogMaskedAttribute_Replaces_Value_With_Provided_Mask_And_PreservedLe { // [LogMasked(Text = "#")] // 123456789 -> "#########" - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedMaskedLogs { CustomMaskedPreservedLength = "123456789" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -326,20 +269,12 @@ public void LogMaskedAttribute_Shows_First_NChars_Then_Replaces_All_With_Custom_ { // [LogMasked(Text = "REMOVED", ShowFirst = 3)] // -> "123_REMOVED_" - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedMaskedLogs { ShowFirstThreeThenCustomMask = "123456789" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -353,20 +288,12 @@ public void LogMaskedAttribute_Shows_First_NChars_Then_Replaces_All_With_Custom_ { // [LogMasked(Text = "REMOVED", ShowFirst = 3,PreserveLength = true)] // -> "123_REMOVED_" - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedMaskedLogs { ShowFirstThreeThenCustomMaskPreservedLengthIgnored = "123456789" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -380,20 +307,12 @@ public void LogMaskedAttribute_Shows_First_NChars_And_Last_NChars_Replaces_Value { // [LogMasked(ShowFirst = 3, ShowLast = 3)] // -> "123***321" - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedMaskedLogs { ShowFirstAndLastThreeAndDefaultMaskInTheMiddle = "12345678987654321" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -407,20 +326,12 @@ public void LogMaskedAttribute_Shows_First_NChars_And_Last_NChars_Replaces_Value { // [LogMasked(ShowFirst = 3, ShowLast = 3)] // -> "123***********321" - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedMaskedLogs { ShowFirstAndLastThreeAndDefaultMaskInTheMiddlePreservedLength = "12345678987654321" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -434,20 +345,12 @@ public void LogMaskedAttribute_Shows_First_NChars_And_Last_NChars_Replaces_Value { // [LogMasked(ShowFirst = 3, ShowLast = 3)] // -> "123*456" - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedMaskedLogs { ShowFirstAndLastThreeAndDefaultMaskInTheMiddlePreservedLength = "123x456" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -461,20 +364,12 @@ public void LogMaskedAttribute_Shows_First_NChars_And_Last_NChars_Then_Replaces_ { // [LogMasked(Text = "REMOVED", ShowFirst = 3, ShowLast = 3)] // 12345678987654321 -> 123_REMOVED_321 - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedMaskedLogs { ShowFirstAndLastThreeAndCustomMaskInTheMiddle = "12345678987654321" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -488,20 +383,12 @@ public void LogMaskedAttribute_Shows_First_NChars_And_Last_NChars_Then_Replaces_ { // [LogMasked(Text = "#", ShowFirst = 3, ShowLast = 3)] // 12345678987654321 -> "123_REMOVED_321" - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedMaskedLogs { ShowFirstAndLastThreeAndCustomMaskInTheMiddle = "12345678987654321" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -515,20 +402,12 @@ public void LogMaskedAttribute_Shows_First_NChars_And_Last_NChars_Then_Replaces_ { // [LogMasked(Text = "#", ShowFirst = 3, ShowLast = 3)] // 12 -> "12" - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedMaskedLogs { ShowFirstAndLastThreeAndCustomMaskInTheMiddle = "12" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -542,20 +421,12 @@ public void LogMaskedAttribute_Shows_First_NChars_And_Last_NChars_Then_Replaces_ { // [LogMasked(Text = "#", ShowFirst = 3, ShowLast = 3)] // 1234 -> "1234" - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedMaskedLogs { ShowFirstAndLastThreeAndCustomMaskInTheMiddle = "1234" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -569,20 +440,12 @@ public void LogMaskedAttribute_Shows_First_NChars_Then_Replaces_All_Other_Chars_ { // [LogMasked(ShowLast = 3)] // 123456789 -> "123***" - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedMaskedLogs { ShowFirstThreeThenDefaultMasked = "123456789" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -596,20 +459,12 @@ public void LogMaskedAttribute_Shows_Last_NChars_Then_Replaces_All_Other_Chars_W { // [LogMasked(Text = "_REMOVED_", ShowLast = 3)] // 123456789 -> "_REMOVED_789" - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedMaskedLogs { ShowLastThreeThenCustomMask = "123456789" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -623,20 +478,12 @@ public void LogMaskedAttribute_Shows_Last_NChars_Then_Replaces_All_Other_Chars_W { // [LogMasked(Text = "_REMOVED_", ShowLast = 3, PreserveLength = true)] // 123456789 -> "_REMOVED_789" - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedMaskedLogs { ShowLastThreeThenCustomMaskPreservedLengthIgnored = "123456789" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -650,20 +497,12 @@ public void LogMaskedAttribute_Shows_Last_NChars_Then_Replaces_All_Other_Chars_W { // [LogMasked(ShowLast = 3)] // 123456789 -> "***789" - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedMaskedLogs { ShowLastThreeThenDefaultMasked = "123456789" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -677,20 +516,12 @@ public void LogMaskedAttribute_Shows_First_NChars_Then_Replaces_All_Other_Chars_ { // [LogMasked(ShowFirst = 3,PreserveLength = true))] // -> "123******" - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedMaskedLogs { ShowFirstThreeThenDefaultMaskedPreservedLength = "123456789" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -704,20 +535,12 @@ public void LogMaskedAttribute_Shows_First_NChars_Then_Replaces_All_Other_Chars_ { // [LogMasked(ShowFirst = 3,PreserveLength = true))] // -> "" - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedMaskedLogs { ShowFirstThreeThenDefaultMaskedPreservedLength = "" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; @@ -732,20 +555,12 @@ public void LogMaskedAttribute_Shows_First_NChars_Then_Replaces_All_Other_Chars_ { // [LogMasked(ShowFirst = 3,PreserveLength = true))] // -> "123" - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedMaskedLogs { ShowFirstThreeThenDefaultMaskedPreservedLength = "123" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; @@ -760,20 +575,12 @@ public void LogMaskedAttribute_Shows_First_NChars_Then_Replaces_All_Other_Chars_ { // [LogMasked(ShowFirst = 3,PreserveLength = true))] // -> "12" - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedMaskedLogs { ShowFirstThreeThenDefaultMaskedPreservedLength = "12" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; @@ -788,20 +595,12 @@ public void LogMaskedAttribute_Shows_Last_NChars_Then_Replaces_All_Other_Chars_W { // [LogMasked(ShowLast = 3,PreserveLength = true))] // -> "******789" - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedMaskedLogs { ShowLastThreeThenDefaultMaskedPreservedLength = "123456789" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -815,20 +614,12 @@ public void LogMaskedAttribute_Shows_Last_NChars_Then_Replaces_All_Other_Chars_W { // [LogMasked(ShowLast = 3,PreserveLength = true))] // -> "123" - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedMaskedLogs { ShowLastThreeThenDefaultMaskedPreservedLength = "123" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -842,20 +633,12 @@ public void LogMaskedAttribute_Shows_Last_NChars_Then_Replaces_All_Other_Chars_W { // [LogMasked(ShowLast = 3,PreserveLength = true))] // -> "12" - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedMaskedLogs { ShowLastThreeThenDefaultMaskedPreservedLength = "12" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -869,20 +652,12 @@ public void LogMaskedAttribute_Shows_First_NChars_And_Last_NChars_Then_Replaces_ { // [LogMasked(Text = "REMOVED", ShowFirst = 3, ShowLast = 3, PreserveLength = true)] // 12345678987654321 -> 123_REMOVED_321 - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedMaskedLogs { ShowFirstAndLastThreeAndCustomMaskInTheMiddlePreservedLengthIgnored = "12345678987654321" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); diff --git a/src/Destructurama.Attributed.Tests/NotLoggedIfDefaultAttributeTests.cs b/src/Destructurama.Attributed.Tests/NotLoggedIfDefaultAttributeTests.cs index 0fc07b4..4eeee69 100644 --- a/src/Destructurama.Attributed.Tests/NotLoggedIfDefaultAttributeTests.cs +++ b/src/Destructurama.Attributed.Tests/NotLoggedIfDefaultAttributeTests.cs @@ -1,6 +1,5 @@ using Destructurama.Attributed.Tests.Support; using NUnit.Framework; -using Serilog; using Serilog.Events; using Shouldly; @@ -72,16 +71,9 @@ private class NotLoggedIfDefaultCustomizedDefaultLogs [Test] public void NotLoggedIfDefault_Uninitialized() { - LogEvent? evt = null; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new NotLoggedIfDefaultCustomizedDefaultLogs(); - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt!.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -123,13 +115,6 @@ public void NotLoggedIfDefault_Uninitialized() [Test] public void NotLoggedIfDefault_Initialized() { - LogEvent? evt = null; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var dateTime = DateTime.UtcNow; var theStruct = new NotLoggedIfDefaultStruct { @@ -155,7 +140,7 @@ public void NotLoggedIfDefault_Initialized() IntegerAsObject = 0 }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt!.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); diff --git a/src/Destructurama.Attributed.Tests/ReplacedAttributeTests.cs b/src/Destructurama.Attributed.Tests/ReplacedAttributeTests.cs index 7ce482c..614cf30 100644 --- a/src/Destructurama.Attributed.Tests/ReplacedAttributeTests.cs +++ b/src/Destructurama.Attributed.Tests/ReplacedAttributeTests.cs @@ -1,6 +1,5 @@ using Destructurama.Attributed.Tests.Support; using NUnit.Framework; -using Serilog; using Serilog.Events; using Shouldly; @@ -43,20 +42,12 @@ public void LogReplacedAttribute_Replaces_First() { // [LogReplaced(@"([a-zA-Z0-9]+)\|([a-zA-Z0-9]+)\|([a-zA-Z0-9]+)", "***|$2|$3")] // 123|456|789 -> "***|456|789" - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedRegexLogs { RegexReplaceFirst = "123|456|789" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -70,20 +61,12 @@ public void LogReplacedAttribute_Replaces_Second() { // [LogReplaced(@"([a-zA-Z0-9]+)\|([a-zA-Z0-9]+)\|([a-zA-Z0-9]+)", "$1|***|$3")] // 123|456|789 -> "123|***|789" - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedRegexLogs { RegexReplaceSecond = "123|456|789" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -97,20 +80,12 @@ public void LogReplacedAttribute_Replaces_Third() { // [LogReplaced(@"([a-zA-Z0-9]+)\|([a-zA-Z0-9]+)\|([a-zA-Z0-9]+)", "$1|$2|***")] // 123|456|789 -> "123|456|***" - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedRegexLogs { RegexReplaceThird = "123|456|789" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -124,20 +99,12 @@ public void LogReplacedAttribute_Replaces_FirstThird() { // [LogReplaced(@"([a-zA-Z0-9]+)\|([a-zA-Z0-9]+)\|([a-zA-Z0-9]+)", "***|$2|****")] // 123|456|789 -> "***|456|****" - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedRegexLogs { RegexReplaceFirstThird = "123|456|789" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); @@ -151,21 +118,13 @@ public void LogReplacedAttribute_Replaces_First_And_Third() { // [LogReplaced(@"([a-zA-Z0-9]+)\|([a-zA-Z0-9]+)\|([a-zA-Z0-9]+)", "***|$2|$3")] // 123|456|789 -> "***|456|789" - - LogEvent evt = null!; - - var log = new LoggerConfiguration() - .Destructure.UsingAttributes() - .WriteTo.Sink(new DelegatingSink(e => evt = e)) - .CreateLogger(); - var customized = new CustomizedRegexLogs { RegexReplaceFirst = "123|456|789", RegexReplaceThird = "123|456|789" }; - log.Information("Here is {@Customized}", customized); + var evt = DelegatingSink.Execute(customized); var sv = (StructureValue)evt.Properties["Customized"]; var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); diff --git a/src/Destructurama.Attributed.Tests/Support/DelegatingSink.cs b/src/Destructurama.Attributed.Tests/Support/DelegatingSink.cs index 95abafe..eac333d 100644 --- a/src/Destructurama.Attributed.Tests/Support/DelegatingSink.cs +++ b/src/Destructurama.Attributed.Tests/Support/DelegatingSink.cs @@ -1,3 +1,4 @@ +using Serilog; using Serilog.Core; using Serilog.Events; @@ -13,4 +14,19 @@ public DelegatingSink(Action write) } public void Emit(LogEvent logEvent) => _write(logEvent); + + public static LogEvent Execute(object obj, string messageTemplate = "Here is {@Customized}", Action? configure = null) + { + LogEvent evt = null!; + + var cfg = new LoggerConfiguration(); + cfg = configure == null ? cfg.Destructure.UsingAttributes() : cfg.Destructure.UsingAttributes(configure); + cfg = cfg.WriteTo.Sink(new DelegatingSink(e => evt = e)); + + var log = cfg.CreateLogger(); + log.Information(messageTemplate, obj); + + return evt; + } + }