@@ -86,7 +86,7 @@ public void Throwing_Accessor_Should_Be_Handled()
8686 [ Test ]
8787 public void TryDestructure_Should_Return_False_When_Called_With_Null ( )
8888 {
89- var policy = new DestructureByIgnoringPolicy ( _ => true , _ => true ) ;
89+ var policy = new DestructureByIgnoringPolicy ( _ => true , ( _ , _ ) => false , _ => true ) ;
9090 policy . TryDestructure ( null ! , null ! , out _ ) . ShouldBeFalse ( ) ;
9191 }
9292
@@ -112,14 +112,148 @@ public void TryDestructure_Should_Work_For_Structs()
112112 sv . Properties [ 0 ] . Value . ShouldBeOfType < ScalarValue > ( ) . Value . ShouldBe ( "Tom" ) ;
113113 }
114114
115- public struct DestructureMeStruct
115+ [ Test ]
116+ public void TryDestructure_Should_Ignore_Property_From_Options ( )
117+ {
118+ // Setup
119+ LogEvent evt = null ! ;
120+
121+ var log = new LoggerConfiguration ( )
122+ . Destructure . ByIgnoring < DestructureMeClass > ( o => o . Ignore ( x => x . Id ) )
123+ . WriteTo . Sink ( new DelegatingSink ( e => evt = e ) )
124+ . CreateLogger ( ) ;
125+ var obj = new DestructureMeClass { Id = 42 } ;
126+
127+ // Execute
128+ log . Information ( "Here is {@Ignored}" , obj ) ;
129+
130+ // Verify
131+ var sv = ( StructureValue ) evt . Properties [ "Ignored" ] ;
132+ sv . Properties . Count . ShouldBe ( 1 ) ;
133+ sv . Properties [ 0 ] . Name . ShouldBe ( "Name" ) ;
134+ sv . Properties [ 0 ] . Value . ShouldBeOfType < ScalarValue > ( ) . Value . ShouldBe ( "Tom" ) ;
135+ }
136+
137+ [ Test ]
138+ public void TryDestructure_Should_Ignore_Null_String ( )
139+ {
140+ // Setup
141+ LogEvent evt = null ! ;
142+
143+ var log = new LoggerConfiguration ( )
144+ . Destructure . ByIgnoring < DestructureMeClass > ( o => o . IgnoreValue ( ( _ , v ) => v is null ) )
145+ . WriteTo . Sink ( new DelegatingSink ( e => evt = e ) )
146+ . CreateLogger ( ) ;
147+ var obj = new DestructureMeClass { Name = null } ;
148+
149+ // Execute
150+ log . Information ( "Here is {@Ignored}" , obj ) ;
151+
152+ // Verify
153+ var sv = ( StructureValue ) evt . Properties [ "Ignored" ] ;
154+ sv . Properties . Count . ShouldBe ( 1 ) ;
155+ sv . Properties [ 0 ] . Name . ShouldBe ( "Id" ) ;
156+ sv . Properties [ 0 ] . Value . ShouldBeOfType < ScalarValue > ( ) . Value . ShouldBe ( 0 ) ;
157+ }
158+
159+ [ Test ]
160+ public void TryDestructure_Should_Ignore_Custom_Value ( )
161+ {
162+ // Setup
163+ LogEvent evt = null ! ;
164+
165+ var log = new LoggerConfiguration ( )
166+ . Destructure . ByIgnoring < DestructureMeClass > ( o => o . IgnoreValue ( ( p , v ) => p . Name is "Id" && v is 42 ) )
167+ . WriteTo . Sink ( new DelegatingSink ( e => evt = e ) )
168+ . CreateLogger ( ) ;
169+ var obj = new DestructureMeClass { Id = 42 } ;
170+
171+ // Execute
172+ log . Information ( "Here is {@Ignored}" , obj ) ;
173+
174+ // Verify
175+ var sv = ( StructureValue ) evt . Properties [ "Ignored" ] ;
176+ sv . Properties . Count . ShouldBe ( 1 ) ;
177+ sv . Properties [ 0 ] . Name . ShouldBe ( "Name" ) ;
178+ sv . Properties [ 0 ] . Value . ShouldBeOfType < ScalarValue > ( ) . Value . ShouldBe ( "Tom" ) ;
179+ }
180+
181+ [ Test ]
182+ public void TryDestructure_Should_Ignore_All_AssignableTo ( )
183+ {
184+ // Setup
185+ LogEvent evt = null ! ;
186+
187+ var log = new LoggerConfiguration ( )
188+ . Destructure . ByIgnoring < IDestructureMe > ( o => o . IgnoreValue ( ( _ , v ) => v is null ) . DestructureAssignableTo ( ) )
189+ . WriteTo . Sink ( new DelegatingSink ( e => evt = e ) )
190+ . CreateLogger ( ) ;
191+ var obj1 = new DestructureMeStruct { Name = null } ;
192+ var obj2 = new DestructureMeClass { Name = null } ;
193+
194+ // Execute
195+ log . Information ( "Here is {@Ignored1} and {@Ignored2}" , obj1 , obj2 ) ;
196+
197+ // Verify
198+ var sv = ( StructureValue ) evt . Properties [ "Ignored1" ] ;
199+ sv . Properties . Count . ShouldBe ( 1 ) ;
200+ sv . Properties [ 0 ] . Name . ShouldBe ( "Id" ) ;
201+ sv . Properties [ 0 ] . Value . ShouldBeOfType < ScalarValue > ( ) . Value . ShouldBe ( 0 ) ;
202+ sv = ( StructureValue ) evt . Properties [ "Ignored2" ] ;
203+ sv . Properties . Count . ShouldBe ( 1 ) ;
204+ sv . Properties [ 0 ] . Name . ShouldBe ( "Id" ) ;
205+ sv . Properties [ 0 ] . Value . ShouldBeOfType < ScalarValue > ( ) . Value . ShouldBe ( 0 ) ;
206+ }
207+
208+ [ Test ]
209+ public void TryDestructure_Should_Ignore_Exact_Type ( )
210+ {
211+ // Setup
212+ LogEvent evt = null ! ;
213+
214+ var log = new LoggerConfiguration ( )
215+ . Destructure . ByIgnoring < DestructureMeClass > ( o => o . IgnoreValue ( ( _ , v ) => v is null ) . DestructureExactType ( ) )
216+ . WriteTo . Sink ( new DelegatingSink ( e => evt = e ) )
217+ . CreateLogger ( ) ;
218+ var obj1 = new DestructureMeStruct { Name = null } ;
219+ var obj2 = new DestructureMeClass { Name = null } ;
220+
221+ // Execute
222+ log . Information ( "Here is {@Ignored1} and {@Ignored2}" , obj1 , obj2 ) ;
223+
224+ // Verify
225+ var sv = ( StructureValue ) evt . Properties [ "Ignored1" ] ;
226+ sv . Properties . Count . ShouldBe ( 2 ) ;
227+ sv . Properties [ 0 ] . Name . ShouldBe ( "Id" ) ;
228+ sv . Properties [ 0 ] . Value . ShouldBeOfType < ScalarValue > ( ) . Value . ShouldBe ( 0 ) ;
229+ sv . Properties [ 1 ] . Name . ShouldBe ( "Name" ) ;
230+ sv . Properties [ 1 ] . Value . ShouldBeOfType < ScalarValue > ( ) . Value . ShouldBeNull ( ) ;
231+
232+ sv = ( StructureValue ) evt . Properties [ "Ignored2" ] ;
233+ sv . Properties . Count . ShouldBe ( 1 ) ;
234+ sv . Properties [ 0 ] . Name . ShouldBe ( "Id" ) ;
235+ sv . Properties [ 0 ] . Value . ShouldBeOfType < ScalarValue > ( ) . Value . ShouldBe ( 0 ) ;
236+ }
237+
238+ public struct DestructureMeStruct : IDestructureMe
116239 {
117240 public DestructureMeStruct ( )
118241 {
119242 }
120243
121244 public int Id { get ; set ; }
122245
123- public string Name { get ; set ; } = "Tom" ;
246+ public string ? Name { get ; set ; } = "Tom" ;
247+ }
248+
249+ public interface IDestructureMe
250+ {
251+ }
252+
253+ public class DestructureMeClass : IDestructureMe
254+ {
255+ public int Id { get ; set ; }
256+
257+ public string ? Name { get ; set ; } = "Tom" ;
124258 }
125259}
0 commit comments