1
- // Copyright 2018 Serilog Contributors
1
+ // Copyright 2018 Serilog Contributors
2
2
//
3
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
4
// you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@ namespace Serilog.Sinks.MSSqlServer
29
29
internal class JsonLogEventFormatter : ITextFormatter
30
30
{
31
31
static readonly JsonValueFormatter ValueFormatter = new JsonValueFormatter ( typeTagName : null ) ;
32
+ private const string COMMA_DELIMITER = "," ;
32
33
33
34
MSSqlServerSinkTraits traits ;
34
35
@@ -52,6 +53,8 @@ public void Format(LogEvent logEvent, TextWriter output)
52
53
53
54
output . Write ( "{" ) ;
54
55
56
+ string precedingDelimiter = "" ;
57
+
55
58
if ( traits . columnOptions . LogEvent . ExcludeStandardColumns == false )
56
59
{
57
60
// The XML Properties column has never included the Standard Columns, but prior
@@ -63,7 +66,6 @@ public void Format(LogEvent logEvent, TextWriter output)
63
66
// whether Standard Columns are written (specifically, the subset of Standard
64
67
// columns that were output by the external JsonFormatter class).
65
68
66
- string precedingDelimiter = "" ;
67
69
var store = traits . columnOptions . Store ;
68
70
69
71
WriteIfPresent ( StandardColumn . TimeStamp ) ;
@@ -77,7 +79,7 @@ void WriteIfPresent(StandardColumn col)
77
79
if ( store . Contains ( col ) )
78
80
{
79
81
output . Write ( precedingDelimiter ) ;
80
- precedingDelimiter = "," ;
82
+ precedingDelimiter = COMMA_DELIMITER ;
81
83
var colData = traits . GetStandardColumnNameAndValue ( col , logEvent ) ;
82
84
JsonValueFormatter . WriteQuotedJsonString ( colData . Key , output ) ;
83
85
output . Write ( ":" ) ;
@@ -88,7 +90,11 @@ void WriteIfPresent(StandardColumn col)
88
90
}
89
91
90
92
if ( logEvent . Properties . Count != 0 )
93
+ {
94
+ output . Write ( precedingDelimiter ) ;
91
95
WriteProperties ( logEvent . Properties , output ) ;
96
+ precedingDelimiter = COMMA_DELIMITER ;
97
+ }
92
98
93
99
var tokensWithFormat = logEvent . MessageTemplate . Tokens
94
100
. OfType < PropertyToken > ( )
@@ -98,6 +104,7 @@ void WriteIfPresent(StandardColumn col)
98
104
99
105
if ( tokensWithFormat . Length != 0 )
100
106
{
107
+ output . Write ( precedingDelimiter ) ;
101
108
WriteRenderings ( tokensWithFormat , logEvent . Properties , output ) ;
102
109
}
103
110
@@ -106,13 +113,13 @@ void WriteIfPresent(StandardColumn col)
106
113
107
114
static void WriteProperties ( IReadOnlyDictionary < string , LogEventPropertyValue > properties , TextWriter output )
108
115
{
109
- output . Write ( ", \" Properties\" :{" ) ;
116
+ output . Write ( "\" Properties\" :{" ) ;
110
117
111
118
string precedingDelimiter = "" ;
112
119
foreach ( var property in properties )
113
120
{
114
121
output . Write ( precedingDelimiter ) ;
115
- precedingDelimiter = "," ;
122
+ precedingDelimiter = COMMA_DELIMITER ;
116
123
JsonValueFormatter . WriteQuotedJsonString ( property . Key , output ) ;
117
124
output . Write ( ':' ) ;
118
125
ValueFormatter . Format ( property . Value , output ) ;
@@ -123,13 +130,13 @@ static void WriteProperties(IReadOnlyDictionary<string, LogEventPropertyValue> p
123
130
124
131
static void WriteRenderings ( IEnumerable < IGrouping < string , PropertyToken > > tokensWithFormat , IReadOnlyDictionary < string , LogEventPropertyValue > properties , TextWriter output )
125
132
{
126
- output . Write ( ", \" Renderings\" :{" ) ;
133
+ output . Write ( "\" Renderings\" :{" ) ;
127
134
128
135
string precedingDelimiter = "" ;
129
136
foreach ( var ptoken in tokensWithFormat )
130
137
{
131
138
output . Write ( precedingDelimiter ) ;
132
- precedingDelimiter = "," ;
139
+ precedingDelimiter = COMMA_DELIMITER ;
133
140
134
141
JsonValueFormatter . WriteQuotedJsonString ( ptoken . Key , output ) ;
135
142
output . Write ( ":[" ) ;
@@ -138,7 +145,7 @@ static void WriteRenderings(IEnumerable<IGrouping<string, PropertyToken>> tokens
138
145
foreach ( var format in ptoken )
139
146
{
140
147
output . Write ( fdelim ) ;
141
- fdelim = "," ;
148
+ fdelim = COMMA_DELIMITER ;
142
149
143
150
output . Write ( "{\" Format\" :" ) ;
144
151
JsonValueFormatter . WriteQuotedJsonString ( format . Format , output ) ;
0 commit comments