11using System ;
22using System . Collections . Generic ;
3+ using System . Collections . Immutable ;
4+ using System . Linq ;
35using VerifyTests ;
46
57namespace Stravaig . Extensions . Logging . Diagnostics . Verify ;
68
79internal class LogEntryConverter : WriteOnlyJsonConverter < IEnumerable < LogEntry > >
810{
11+ internal const string DefaultNonDeterministicPropertyValueSubstitute = "*** NONDETERMINISTIC ***" ;
912 private readonly Settings _settings ;
13+ private readonly IReadOnlySet < string > _nonDeterministicPropertyNames ;
14+ private readonly string _nonDeterministicPropertyValueSubstitute ;
1015
1116 public LogEntryConverter ( )
12- : this ( Settings . Default )
17+ : this ( Settings . Default , ImmutableHashSet < string > . Empty , DefaultNonDeterministicPropertyValueSubstitute )
1318 {
1419
1520 }
1621
17- public LogEntryConverter ( Settings settings )
22+ public LogEntryConverter ( Settings settings , IReadOnlySet < string > nonDeterministicPropertyNames , string nonDeterministicPropertyValueSubstitute )
1823 {
1924 _settings = settings ;
25+ _nonDeterministicPropertyNames = nonDeterministicPropertyNames ;
26+ _nonDeterministicPropertyValueSubstitute = nonDeterministicPropertyValueSubstitute ;
2027 }
2128
2229 private class Context
2330 {
24- internal Settings Settings { get ; }
31+ private Settings Settings { get ; }
2532 internal VerifyJsonWriter Writer { get ; }
2633 private int _sequence ;
2734 internal int CadenceOffset { get ; set ; }
@@ -35,6 +42,9 @@ private class Context
3542 internal bool IsWritingExceptionType => Using ( Settings . ExceptionType ) ;
3643 internal bool IsWritingInnerException => Using ( Settings . InnerException ) ;
3744 internal bool IsWritingStackTrace => Using ( Settings . StackTrace ) ;
45+ internal bool IsWritingProperties => Using ( Settings . Properties ) ;
46+ internal bool IsWritingMessageTemplate => Using ( Settings . MessageTemplate ) ;
47+ internal bool HideNonDeterministicProperties => Using ( Settings . HideNonDeterministicProperties ) ;
3848
3949 internal int CurrentSequence ( LogEntry logEntry ) =>
4050 Using ( Settings . KeepSequenceCadence )
@@ -75,7 +85,9 @@ public override void Write(VerifyJsonWriter writer, IEnumerable<LogEntry> logEnt
7585 WriteSequence ( ctx , logEntry ) ;
7686 WriteLogLevel ( ctx , logEntry ) ;
7787 WriteCategoryName ( ctx , logEntry ) ;
88+ WriteMessageTemplate ( ctx , logEntry ) ;
7889 WriteFormattedMessage ( ctx , logEntry ) ;
90+ WriteProperties ( ctx , logEntry ) ;
7991 WriteException ( ctx , logEntry ) ;
8092
8193 writer . WriteEndObject ( ) ;
@@ -85,6 +97,37 @@ public override void Write(VerifyJsonWriter writer, IEnumerable<LogEntry> logEnt
8597 writer . WriteEndArray ( ) ;
8698 }
8799
100+ private void WriteProperties ( Context ctx , LogEntry logEntry )
101+ {
102+ if ( ctx . IsWritingProperties )
103+ {
104+ ctx . Writer . WritePropertyName ( nameof ( Settings . Properties ) ) ;
105+ ctx . Writer . WriteStartObject ( ) ;
106+ foreach ( var property in logEntry . Properties . OrderBy ( p => p . Key ) )
107+ {
108+ bool isNonDeterministic = _nonDeterministicPropertyNames . Contains ( property . Key ) ;
109+ if ( isNonDeterministic && ctx . HideNonDeterministicProperties )
110+ continue ;
111+ ctx . Writer . WritePropertyName ( property . Key ) ;
112+
113+ if ( isNonDeterministic )
114+ ctx . Writer . WriteValue ( _nonDeterministicPropertyValueSubstitute ) ;
115+ else
116+ ctx . Writer . WriteValue ( property . Value ) ;
117+ }
118+ ctx . Writer . WriteEndObject ( ) ;
119+ }
120+ }
121+
122+ private void WriteMessageTemplate ( Context ctx , LogEntry logEntry )
123+ {
124+ if ( ctx . IsWritingMessageTemplate )
125+ {
126+ ctx . Writer . WritePropertyName ( nameof ( Settings . MessageTemplate ) ) ;
127+ ctx . Writer . WriteValue ( logEntry . OriginalMessage ) ;
128+ }
129+ }
130+
88131 private static void WriteException ( Context ctx , LogEntry logEntry )
89132 {
90133 var ex = logEntry . Exception ;
0 commit comments