88
99namespace Serilog . Sinks . SqlServer ;
1010
11+ /// <summary>
12+ /// Provides utility methods for writing Serilog log event data to JSON format.
13+ /// </summary>
1114public static class JsonWriter
1215{
16+ /// <summary>
17+ /// Writes an exception to a JSON string representation.
18+ /// </summary>
19+ /// <param name="exception">The exception to serialize. Can be null.</param>
20+ /// <returns>A JSON string containing exception details, or null if the exception is null.</returns>
21+ /// <remarks>
22+ /// The JSON output includes the exception message, type, full text, error codes, source, and method information.
23+ /// Aggregate exceptions with a single inner exception are automatically flattened.
24+ /// </remarks>
1325 public static string ? WriteException ( Exception ? exception )
1426 {
1527 if ( exception == null )
@@ -77,6 +89,12 @@ public static class JsonWriter
7789
7890 }
7991
92+ /// <summary>
93+ /// Writes a collection of log event properties to a JSON string representation.
94+ /// </summary>
95+ /// <param name="properties">The dictionary of log event properties to serialize. Can be null or empty.</param>
96+ /// <param name="ignored">An optional set of property names to exclude from the output.</param>
97+ /// <returns>A JSON string containing the properties, or null if there are no properties to write or all properties are ignored.</returns>
8098 public static string ? WriteProperties ( IReadOnlyDictionary < string , LogEventPropertyValue > ? properties , HashSet < string > ? ignored = null )
8199 {
82100 // no properties to write
@@ -117,6 +135,11 @@ public static class JsonWriter
117135#endif
118136 }
119137
138+ /// <summary>
139+ /// Writes a single log event property value to a JSON string representation.
140+ /// </summary>
141+ /// <param name="value">The log event property value to serialize. Can be null.</param>
142+ /// <returns>A JSON string containing the property value, or null if the value is null.</returns>
120143 public static string ? WritePropertyValue ( LogEventPropertyValue value )
121144 {
122145 if ( value == null )
@@ -141,8 +164,15 @@ public static class JsonWriter
141164#endif
142165 }
143166
144-
145- public static void WritePropertyValue ( Utf8JsonWriter writer , LogEventPropertyValue value )
167+ /// <summary>
168+ /// Writes a log event property value to the specified JSON writer.
169+ /// </summary>
170+ /// <param name="writer">The UTF-8 JSON writer to write to.</param>
171+ /// <param name="value">The log event property value to write.</param>
172+ /// <remarks>
173+ /// Handles scalar values, sequences, structures, and dictionaries appropriately.
174+ /// </remarks>
175+ private static void WritePropertyValue ( Utf8JsonWriter writer , LogEventPropertyValue value )
146176 {
147177 if ( value is ScalarValue scalarValue )
148178 WriteScalarValue ( writer , scalarValue ) ;
@@ -156,7 +186,12 @@ public static void WritePropertyValue(Utf8JsonWriter writer, LogEventPropertyVal
156186 writer . WriteStringValue ( value . ToString ( ) ) ;
157187 }
158188
159- public static void WriteDictionaryValue ( Utf8JsonWriter writer , DictionaryValue dictionaryValue )
189+ /// <summary>
190+ /// Writes a dictionary value to the specified JSON writer as a JSON object.
191+ /// </summary>
192+ /// <param name="writer">The UTF-8 JSON writer to write to.</param>
193+ /// <param name="dictionaryValue">The dictionary value to write.</param>
194+ private static void WriteDictionaryValue ( Utf8JsonWriter writer , DictionaryValue dictionaryValue )
160195 {
161196 writer . WriteStartObject ( ) ;
162197 foreach ( var kvp in dictionaryValue . Elements )
@@ -170,7 +205,12 @@ public static void WriteDictionaryValue(Utf8JsonWriter writer, DictionaryValue d
170205 writer . WriteEndObject ( ) ;
171206 }
172207
173- public static void WriteStructureValue ( Utf8JsonWriter writer , StructureValue structureValue )
208+ /// <summary>
209+ /// Writes a structure value to the specified JSON writer as a JSON object.
210+ /// </summary>
211+ /// <param name="writer">The UTF-8 JSON writer to write to.</param>
212+ /// <param name="structureValue">The structure value to write.</param>
213+ private static void WriteStructureValue ( Utf8JsonWriter writer , StructureValue structureValue )
174214 {
175215 writer . WriteStartObject ( ) ;
176216 foreach ( var prop in structureValue . Properties )
@@ -181,7 +221,12 @@ public static void WriteStructureValue(Utf8JsonWriter writer, StructureValue str
181221 writer . WriteEndObject ( ) ;
182222 }
183223
184- public static void WriteSequenceValue ( Utf8JsonWriter writer , SequenceValue sequenceValue )
224+ /// <summary>
225+ /// Writes a sequence value to the specified JSON writer as a JSON array.
226+ /// </summary>
227+ /// <param name="writer">The UTF-8 JSON writer to write to.</param>
228+ /// <param name="sequenceValue">The sequence value to write.</param>
229+ private static void WriteSequenceValue ( Utf8JsonWriter writer , SequenceValue sequenceValue )
185230 {
186231 writer . WriteStartArray ( ) ;
187232 foreach ( var item in sequenceValue . Elements )
@@ -191,7 +236,15 @@ public static void WriteSequenceValue(Utf8JsonWriter writer, SequenceValue seque
191236 writer . WriteEndArray ( ) ;
192237 }
193238
194- public static void WriteScalarValue ( Utf8JsonWriter writer , ScalarValue scalarValue )
239+ /// <summary>
240+ /// Writes a scalar value to the specified JSON writer with appropriate JSON typing.
241+ /// </summary>
242+ /// <param name="writer">The UTF-8 JSON writer to write to.</param>
243+ /// <param name="scalarValue">The scalar value to write.</param>
244+ /// <remarks>
245+ /// Handles all primitive types, date/time types, numeric types, and falls back to JSON serialization for unknown types.
246+ /// </remarks>
247+ private static void WriteScalarValue ( Utf8JsonWriter writer , ScalarValue scalarValue )
195248 {
196249 if ( scalarValue . Value == null )
197250 {
0 commit comments