22
22
using Serilog . Formatting . Json ;
23
23
using Serilog . Parsing ;
24
24
using Serilog . Sinks . Seq . Conventions ;
25
- using Serilog . Sinks . Seq . Formatting ;
26
-
27
25
// ReSharper disable MemberCanBePrivate.Global
28
26
// ReSharper disable PossibleMultipleEnumeration
29
27
@@ -36,82 +34,43 @@ namespace Serilog.Sinks.Seq;
36
34
/// implicit SerilogTracing span support.</remarks>
37
35
public sealed class SeqCompactJsonFormatter : ITextFormatter
38
36
{
39
- static readonly IDottedPropertyNameConvention DefaultDottedPropertyNameConvention =
40
- new UnflattenDottedPropertyNames ( ) ;
41
-
42
- readonly JsonValueFormatter _valueFormatter = new ( "$type" ) ;
37
+ readonly JsonValueFormatter _valueFormatter ;
43
38
readonly IFormatProvider _formatProvider ;
44
39
readonly IDottedPropertyNameConvention _dottedPropertyNameConvention ;
45
40
46
41
/// <summary>
47
- /// Create a <see cref="SeqCompactJsonFormatter"/>.
48
- /// </summary>
49
- /// <param name="formatProvider">An <see cref="IFormatProvider"/> that will be used to render log event tokens.</param>
50
- public SeqCompactJsonFormatter ( IFormatProvider ? formatProvider = null )
51
- : this ( formatProvider , false )
52
- {
53
- }
54
-
55
- /// <summary>
56
- /// Create a <see cref="SeqCompactJsonFormatter"/>.
42
+ /// Construct a <see cref="SeqCompactJsonFormatter"/>.
57
43
/// </summary>
44
+ /// <param name="valueFormatter">A value formatter for <see cref="LogEventPropertyValue"/>s on the event.</param>
58
45
/// <param name="formatProvider">An <see cref="IFormatProvider"/> that will be used to render log event tokens.</param>
59
46
/// <param name="preserveDottedPropertyNames">If <c langword="true"/>, log event property names that
60
47
/// contain <c>.</c> will be sent to Seq as-is. Otherwise, properties with dotted names will be converted
61
48
/// into nested objects.</param>
62
- public SeqCompactJsonFormatter ( IFormatProvider ? formatProvider , bool preserveDottedPropertyNames )
49
+ public SeqCompactJsonFormatter ( IFormatProvider ? formatProvider = null , JsonValueFormatter ? valueFormatter = null , bool preserveDottedPropertyNames = false )
63
50
{
64
51
_formatProvider = formatProvider ?? CultureInfo . InvariantCulture ;
52
+ _valueFormatter = valueFormatter ?? new ( "$type" ) ;
65
53
_dottedPropertyNameConvention = preserveDottedPropertyNames
66
54
? new PreserveDottedPropertyNames ( )
67
55
: new UnflattenDottedPropertyNames ( ) ;
68
56
}
69
57
70
58
/// <summary>
71
- /// Format the log event into the output. Subsequent events will be newline-delimited.
59
+ /// Format the log event into the output. Successive events will be newline-delimited.
72
60
/// </summary>
73
61
/// <param name="logEvent">The event to format.</param>
74
62
/// <param name="output">The output.</param>
75
63
public void Format ( LogEvent logEvent , TextWriter output )
76
- {
77
- FormatEvent ( logEvent , output , _valueFormatter , _formatProvider , _dottedPropertyNameConvention ) ;
78
- output . WriteLine ( ) ;
79
- }
80
-
81
- /// <summary>
82
- /// Format the log event into the output.
83
- /// </summary>
84
- /// <param name="logEvent">The event to format.</param>
85
- /// <param name="output">The output.</param>
86
- /// <param name="valueFormatter">A value formatter for <see cref="LogEventPropertyValue"/>s on the event.</param>
87
- /// <param name="formatProvider">An <see cref="IFormatProvider"/> that will be used to render log event tokens.</param>
88
- public static void FormatEvent ( LogEvent logEvent , TextWriter output , JsonValueFormatter valueFormatter , IFormatProvider formatProvider )
89
- {
90
- FormatEvent ( logEvent , output , valueFormatter , formatProvider , DefaultDottedPropertyNameConvention ) ;
91
- }
92
-
93
- static void FormatEvent ( LogEvent logEvent , TextWriter output , JsonValueFormatter valueFormatter , IFormatProvider formatProvider , IDottedPropertyNameConvention dottedPropertyNameConvention )
94
64
{
95
65
if ( logEvent == null ) throw new ArgumentNullException ( nameof ( logEvent ) ) ;
96
66
if ( output == null ) throw new ArgumentNullException ( nameof ( output ) ) ;
97
- if ( valueFormatter == null ) throw new ArgumentNullException ( nameof ( valueFormatter ) ) ;
98
67
99
68
output . Write ( "{\" @t\" :\" " ) ;
100
69
output . Write ( logEvent . Timestamp . UtcDateTime . ToString ( "O" ) ) ;
101
70
102
71
output . Write ( "\" ,\" @mt\" :" ) ;
103
72
JsonValueFormatter . WriteQuotedJsonString ( logEvent . MessageTemplate . Text , output ) ;
104
73
105
- if ( ! formatProvider . Equals ( CultureInfo . InvariantCulture ) )
106
- {
107
- // `@m` is normally created during ingestion, however, it must be sent from the client
108
- // to honour non-default IFormatProviders
109
- output . Write ( ",\" @m\" :" ) ;
110
- JsonValueFormatter . WriteQuotedJsonString (
111
- CleanMessageTemplateFormatter . Format ( logEvent . MessageTemplate , logEvent . Properties , formatProvider ) ,
112
- output ) ;
113
- }
114
-
115
74
var tokensWithFormat = logEvent . MessageTemplate . Tokens
116
75
. OfType < PropertyToken > ( )
117
76
. Where ( pt => pt . Format != null ) ;
@@ -127,9 +86,10 @@ static void FormatEvent(LogEvent logEvent, TextWriter output, JsonValueFormatter
127
86
output . Write ( delim ) ;
128
87
delim = "," ;
129
88
var space = new StringWriter ( ) ;
130
- r . Render ( logEvent . Properties , space , formatProvider ) ;
89
+ r . Render ( logEvent . Properties , space , _formatProvider ) ;
131
90
JsonValueFormatter . WriteQuotedJsonString ( space . ToString ( ) , output ) ;
132
91
}
92
+
133
93
output . Write ( ']' ) ;
134
94
}
135
95
@@ -189,7 +149,7 @@ static void FormatEvent(LogEvent logEvent, TextWriter output, JsonValueFormatter
189
149
}
190
150
}
191
151
192
- var properties = dottedPropertyNameConvention . ProcessDottedPropertyNames ( logEvent . Properties ) ;
152
+ var properties = _dottedPropertyNameConvention . ProcessDottedPropertyNames ( logEvent . Properties ) ;
193
153
foreach ( var property in properties )
194
154
{
195
155
var name = property . Key ;
@@ -206,9 +166,9 @@ static void FormatEvent(LogEvent logEvent, TextWriter output, JsonValueFormatter
206
166
output . Write ( ',' ) ;
207
167
JsonValueFormatter . WriteQuotedJsonString ( name , output ) ;
208
168
output . Write ( ':' ) ;
209
- valueFormatter . Format ( property . Value , output ) ;
169
+ _valueFormatter . Format ( property . Value , output ) ;
210
170
}
211
171
212
172
output . Write ( '}' ) ;
213
173
}
214
- }
174
+ }
0 commit comments