Skip to content

Commit 0ebe3c2

Browse files
committed
Refactor JSON indentation to use spaces only
Replaces the previous indentSize and useSpacesForIndent options with a single spacesPerIndent parameter, enforcing space-based indentation for pretty-printed JSON. Updates all related constructors, method signatures, and tests to reflect this change, simplifying configuration and code paths.
1 parent 84cf276 commit 0ebe3c2

File tree

6 files changed

+34
-77
lines changed

6 files changed

+34
-77
lines changed

Serilog.Sinks.RichTextBox.WinForms.Colored.Test/Integration/JsonFormattingTests.cs

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ public void PrettyPrintJson_FormatsNestedObjectsWithIndentation()
107107
var options = new RichTextBoxSinkOptions(
108108
theme: _defaultTheme,
109109
prettyPrintJson: true,
110-
indentSize: 4,
111-
useSpacesForIndent: true);
110+
spacesPerIndent: 4,
112111

113112
var result = RenderAndGetText(logEvent, "{Message:l}", options);
114113
var expected = "{\n \"Id\": 123,\n \"Name\": \"test\",\n \"$type\": \"MyObj\"\n}";
@@ -129,8 +128,7 @@ public void PrettyPrintJson_FormatsArraysWithIndentation()
129128
var options = new RichTextBoxSinkOptions(
130129
theme: _defaultTheme,
131130
prettyPrintJson: true,
132-
indentSize: 4,
133-
useSpacesForIndent: true);
131+
spacesPerIndent: 4,
134132

135133
var result = RenderAndGetText(logEvent, "{Message:l}", options);
136134
var expected = "[\n 1,\n 2,\n 3\n]";
@@ -151,8 +149,7 @@ public void PrettyPrintJson_FormatsDictionariesWithIndentation()
151149
var options = new RichTextBoxSinkOptions(
152150
theme: _defaultTheme,
153151
prettyPrintJson: true,
154-
indentSize: 4,
155-
useSpacesForIndent: true);
152+
spacesPerIndent: 4,
156153

157154
var result = RenderAndGetText(logEvent, "{Message:l}", options);
158155
var expected = "{\n \"a\": 1,\n \"b\": \"hello\"\n}";
@@ -176,8 +173,7 @@ public void PrettyPrintJson_FormatsNestedStructures()
176173
var options = new RichTextBoxSinkOptions(
177174
theme: _defaultTheme,
178175
prettyPrintJson: true,
179-
indentSize: 4,
180-
useSpacesForIndent: true);
176+
spacesPerIndent: 4,
181177

182178
var result = RenderAndGetText(logEvent, "{Message:l}", options);
183179
var expected = "{\n \"Inner\": {\n \"Value\": 42,\n \"$type\": \"Inner\"\n },\n \"Name\": \"test\",\n \"$type\": \"Outer\"\n}";
@@ -195,8 +191,7 @@ public void PrettyPrintJson_EmptyCollectionsFormatCorrectly()
195191
var options = new RichTextBoxSinkOptions(
196192
theme: _defaultTheme,
197193
prettyPrintJson: true,
198-
indentSize: 4,
199-
useSpacesForIndent: true);
194+
spacesPerIndent: 4,
200195

201196
var result = RenderAndGetText(logEvent, "{Message:l}", options);
202197
Assert.Contains("Array: []", result);
@@ -216,16 +211,15 @@ public void PrettyPrintJson_UsesTabsWhenConfigured()
216211
var options = new RichTextBoxSinkOptions(
217212
theme: _defaultTheme,
218213
prettyPrintJson: true,
219-
indentSize: 4,
220-
useSpacesForIndent: false);
214+
spacesPerIndent: 4,
221215

222216
var result = RenderAndGetText(logEvent, "{Message:l}", options);
223217
var expected = "{\n\t\t\t\t\"Id\": 123,\n\t\t\t\t\"$type\": \"MyObj\"\n}";
224218
Assert.Equal(expected, result);
225219
}
226220

227221
[Fact]
228-
public void PrettyPrintJson_RespectsIndentSize()
222+
public void PrettyPrintJson_RespectsSpacesPerIndent()
229223
{
230224
var prop = new LogEventProperty("Test", new StructureValue(new[]
231225
{
@@ -236,33 +230,13 @@ public void PrettyPrintJson_RespectsIndentSize()
236230
var options = new RichTextBoxSinkOptions(
237231
theme: _defaultTheme,
238232
prettyPrintJson: true,
239-
indentSize: 2,
240-
useSpacesForIndent: true);
233+
spacesPerIndent: 2);
241234

242235
var result = RenderAndGetText(logEvent, "{Message:l}", options);
243236
var expected = "{\n \"Id\": 123,\n \"$type\": \"MyObj\"\n}";
244237
Assert.Equal(expected, result);
245238
}
246239

247-
[Fact]
248-
public void PrettyPrintJson_RespectsIndentSizeWithTabs()
249-
{
250-
var prop = new LogEventProperty("Test", new StructureValue(new[]
251-
{
252-
new LogEventProperty("Id", new ScalarValue(123))
253-
}, "MyObj"));
254-
var logEvent = new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null, _parser.Parse("{Test:j}"), new[] { prop });
255-
256-
var options = new RichTextBoxSinkOptions(
257-
theme: _defaultTheme,
258-
prettyPrintJson: true,
259-
indentSize: 2,
260-
useSpacesForIndent: false);
261-
262-
var result = RenderAndGetText(logEvent, "{Message:l}", options);
263-
var expected = "{\n\t\t\"Id\": 123,\n\t\t\"$type\": \"MyObj\"\n}";
264-
Assert.Equal(expected, result);
265-
}
266240

267241
[Fact]
268242
public void CompactJson_StillWorksByDefault()
@@ -311,8 +285,7 @@ public void PrettyPrintJson_DictionaryWithNullKey()
311285
var options = new RichTextBoxSinkOptions(
312286
theme: _defaultTheme,
313287
prettyPrintJson: true,
314-
indentSize: 4,
315-
useSpacesForIndent: true);
288+
spacesPerIndent: 4,
316289

317290
var result = RenderAndGetText(logEvent, "{Message:l}", options);
318291
Assert.Contains("\"null\"", result);
@@ -332,8 +305,7 @@ public void PrettyPrintJson_DictionaryWithNonStringKey()
332305
var options = new RichTextBoxSinkOptions(
333306
theme: _defaultTheme,
334307
prettyPrintJson: true,
335-
indentSize: 4,
336-
useSpacesForIndent: true);
308+
spacesPerIndent: 4,
337309

338310
var result = RenderAndGetText(logEvent, "{Message:l}", options);
339311
Assert.Contains("\"123\"", result);

Serilog.Sinks.RichTextBox.WinForms.Colored/RichTextBoxSinkLoggerConfigurationExtensions.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ public static class RichTextBoxSinkLoggerConfigurationExtensions
4646
/// <param name="minimumLogEventLevel">Minimum log level for events to be written.</param>
4747
/// <param name="levelSwitch">Optional switch to change the minimum log level at runtime.</param>
4848
/// <param name="prettyPrintJson">If <c>true</c>, formats JSON values with indentation and line breaks. Defaults to <c>false</c>.</param>
49-
/// <param name="indentSize">Number of spaces per indentation level when pretty printing JSON. Defaults to 4. Must be between 1 and 16.</param>
50-
/// <param name="useSpacesForIndent">If <c>true</c> (default), uses spaces for indentation; otherwise uses tabs.</param>
49+
/// <param name="spacesPerIndent">Number of spaces per indentation level when pretty printing JSON. Defaults to 4. Must be between 1 and 16.</param>
5150
/// <returns>The logger configuration, for chaining.</returns>
5251
public static LoggerConfiguration RichTextBox(
5352
this LoggerSinkConfiguration sinkConfiguration,
@@ -61,12 +60,11 @@ public static LoggerConfiguration RichTextBox(
6160
LogEventLevel minimumLogEventLevel = LogEventLevel.Verbose,
6261
LoggingLevelSwitch? levelSwitch = null,
6362
bool prettyPrintJson = false,
64-
int indentSize = 4,
65-
bool useSpacesForIndent = true)
63+
int spacesPerIndent = 4)
6664
{
6765
var appliedTheme = theme ?? ThemePresets.Literate;
6866
var appliedFormatProvider = formatProvider ?? CultureInfo.InvariantCulture;
69-
var options = new RichTextBoxSinkOptions(appliedTheme, autoScroll, maxLogLines, outputTemplate, appliedFormatProvider, prettyPrintJson, indentSize, useSpacesForIndent);
67+
var options = new RichTextBoxSinkOptions(appliedTheme, autoScroll, maxLogLines, outputTemplate, appliedFormatProvider, prettyPrintJson, spacesPerIndent);
7068
var renderer = new TemplateRenderer(appliedTheme, outputTemplate, appliedFormatProvider, options);
7169
richTextBoxSink = new RichTextBoxSink(richTextBoxControl, options, renderer);
7270
return sinkConfiguration.Sink(richTextBoxSink, minimumLogEventLevel, levelSwitch);
@@ -85,8 +83,7 @@ public static LoggerConfiguration RichTextBox(
8583
/// <param name="minimumLogEventLevel">Minimum log level for events to be written.</param>
8684
/// <param name="levelSwitch">Optional switch to change the minimum log level at runtime.</param>
8785
/// <param name="prettyPrintJson">If <c>true</c>, formats JSON values with indentation and line breaks. Defaults to <c>false</c>.</param>
88-
/// <param name="indentSize">Number of spaces per indentation level when pretty printing JSON. Defaults to 4. Must be between 1 and 16.</param>
89-
/// <param name="useSpacesForIndent">If <c>true</c> (default), uses spaces for indentation; otherwise uses tabs.</param>
86+
/// <param name="spacesPerIndent">Number of spaces per indentation level when pretty printing JSON. Defaults to 4. Must be between 1 and 16.</param>
9087
/// <returns>The logger configuration, for chaining.</returns>
9188
public static LoggerConfiguration RichTextBox(
9289
this LoggerSinkConfiguration sinkConfiguration,
@@ -99,8 +96,7 @@ public static LoggerConfiguration RichTextBox(
9996
LogEventLevel minimumLogEventLevel = LogEventLevel.Verbose,
10097
LoggingLevelSwitch? levelSwitch = null,
10198
bool prettyPrintJson = false,
102-
int indentSize = 4,
103-
bool useSpacesForIndent = true)
99+
int spacesPerIndent = 4)
104100
{
105101
return RichTextBox(
106102
sinkConfiguration,
@@ -114,8 +110,7 @@ public static LoggerConfiguration RichTextBox(
114110
minimumLogEventLevel,
115111
levelSwitch,
116112
prettyPrintJson,
117-
indentSize,
118-
useSpacesForIndent);
113+
spacesPerIndent);
119114
}
120115
}
121116
}

Serilog.Sinks.RichTextBox.WinForms.Colored/Sinks/RichTextBoxForms/Formatting/ValueFormatterState.cs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,36 @@ namespace Serilog.Sinks.RichTextBoxForms.Formatting
2222
{
2323
public readonly struct ValueFormatterState
2424
{
25-
public ValueFormatterState(IRtfCanvas canvas, string format, bool isLiteral, int indentLevel = 0, bool useSpacesForIndent = true, int indentSize = 4, bool isTopLevel = true)
25+
public ValueFormatterState(IRtfCanvas canvas, string format, bool isLiteral, int indentLevel = 0, int spacesPerIndent = 4, bool isTopLevel = true)
2626
{
2727
Canvas = canvas;
2828
Format = format;
2929
IsLiteral = isLiteral;
3030
IndentLevel = indentLevel;
31-
UseSpacesForIndent = useSpacesForIndent;
32-
IndentSize = indentSize;
31+
SpacesPerIndent = spacesPerIndent;
3332
IsTopLevel = isTopLevel;
3433
}
3534

3635
public string Format { get; }
3736
public bool IsLiteral { get; }
3837
public IRtfCanvas Canvas { get; }
3938
public int IndentLevel { get; }
40-
public bool UseSpacesForIndent { get; }
41-
public int IndentSize { get; }
39+
public int SpacesPerIndent { get; }
4240
public bool IsTopLevel { get; }
4341

4442
public ValueFormatterState Next(string? format = null)
4543
{
46-
return new ValueFormatterState(Canvas, format ?? Format, IsLiteral, IndentLevel, UseSpacesForIndent, IndentSize, false);
44+
return new ValueFormatterState(Canvas, format ?? Format, IsLiteral, IndentLevel, SpacesPerIndent, false);
4745
}
4846

4947
public ValueFormatterState ToIndentUp()
5048
{
51-
return new ValueFormatterState(Canvas, Format, IsLiteral, IndentLevel + 1, UseSpacesForIndent, IndentSize, false);
49+
return new ValueFormatterState(Canvas, Format, IsLiteral, IndentLevel + 1, SpacesPerIndent, false);
5250
}
5351

5452
public ValueFormatterState ToIndentDown()
5553
{
56-
return new ValueFormatterState(Canvas, Format, IsLiteral, IndentLevel > 0 ? IndentLevel - 1 : 0, UseSpacesForIndent, IndentSize, false);
54+
return new ValueFormatterState(Canvas, Format, IsLiteral, IndentLevel > 0 ? IndentLevel - 1 : 0, SpacesPerIndent, false);
5755
}
5856

5957
public string GetIndentation()
@@ -63,10 +61,7 @@ public string GetIndentation()
6361
return string.Empty;
6462
}
6563

66-
var totalIndentUnits = IndentLevel * IndentSize;
67-
return UseSpacesForIndent
68-
? new string(' ', totalIndentUnits)
69-
: new string('\t', totalIndentUnits);
64+
return new string(' ', IndentLevel * SpacesPerIndent);
7065
}
7166
}
7267
}

Serilog.Sinks.RichTextBox.WinForms.Colored/Sinks/RichTextBoxForms/Rendering/MessageTemplateTokenRenderer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ public MessageTemplateTokenRenderer(Theme theme, PropertyToken token, IFormatPro
3535
var isJson = token.Format?.Contains("j") == true;
3636

3737
ValueFormatter valueFormatter = isJson
38-
? new JsonValueFormatter(theme, formatProvider, options?.PrettyPrintJson ?? false, options?.IndentSize ?? 4, options?.UseSpacesForIndent ?? true)
39-
: new DisplayValueFormatter(theme, formatProvider, options?.PrettyPrintJson ?? false, options?.IndentSize ?? 4, options?.UseSpacesForIndent ?? true);
38+
? new JsonValueFormatter(theme, formatProvider, options?.PrettyPrintJson ?? false, options?.SpacesPerIndent ?? 4, true)
39+
: new DisplayValueFormatter(theme, formatProvider, options?.PrettyPrintJson ?? false, options?.SpacesPerIndent ?? 4, true);
4040

4141
_renderer = new MessageTemplateRenderer(theme, valueFormatter, isLiteral);
4242
}

Serilog.Sinks.RichTextBox.WinForms.Colored/Sinks/RichTextBoxForms/Rendering/PropertiesTokenRenderer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ public class PropertiesTokenRenderer : ITokenRenderer
3535
public PropertiesTokenRenderer(Theme theme, PropertyToken token, MessageTemplate outputTemplate, IFormatProvider? formatProvider, RichTextBoxSinkOptions? options = null)
3636
{
3737
_valueFormatter = token.Format?.Contains("j") == true
38-
? new JsonValueFormatter(theme, formatProvider, options?.PrettyPrintJson ?? false, options?.IndentSize ?? 4, options?.UseSpacesForIndent ?? true)
39-
: new DisplayValueFormatter(theme, formatProvider, options?.PrettyPrintJson ?? false, options?.IndentSize ?? 4, options?.UseSpacesForIndent ?? true);
38+
? new JsonValueFormatter(theme, formatProvider, options?.PrettyPrintJson ?? false, options?.SpacesPerIndent ?? 4, true)
39+
: new DisplayValueFormatter(theme, formatProvider, options?.PrettyPrintJson ?? false, options?.SpacesPerIndent ?? 4, true);
4040

4141
_outputTemplateProperties = new HashSet<string>(
4242
outputTemplate.Tokens.OfType<PropertyToken>().Select(p => p.PropertyName));

Serilog.Sinks.RichTextBox.WinForms.Colored/Sinks/RichTextBoxForms/RichTextBoxSinkOptions.cs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class RichTextBoxSinkOptions
2626
{
2727
private const string DefaultOutputTemplate = "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}";
2828
private int _maxLogLines;
29-
private int _indentSize;
29+
private int _spacesPerIndent;
3030

3131
/// <summary>
3232
/// Creates a new collection of options that control the behavior and appearance of a
@@ -38,26 +38,23 @@ public class RichTextBoxSinkOptions
3838
/// <param name="outputTemplate">Serilog output template that controls textual formatting of each log event.</param>
3939
/// <param name="formatProvider">Optional culture-specific or custom formatting provider used when rendering scalar values; <c>null</c> for the invariant culture.</param>
4040
/// <param name="prettyPrintJson">When <c>true</c>, formats JSON values with indentation and line breaks for better readability. Defaults to <c>false</c>.</param>
41-
/// <param name="indentSize">Number of indentation units per indentation level when pretty printing JSON. When using spaces, this is the number of spaces; when using tabs, this is the number of tabs. Defaults to 4. Must be between 1 and 16.</param>
42-
/// <param name="useSpacesForIndent">When <c>true</c> (default), uses spaces for indentation; otherwise uses tabs.</param>
41+
/// <param name="spacesPerIndent">Number of spaces per indentation level when pretty printing JSON. Defaults to 4. Must be between 1 and 16.</param>
4342
public RichTextBoxSinkOptions(
4443
Theme theme,
4544
bool autoScroll = true,
4645
int maxLogLines = 256,
4746
string outputTemplate = DefaultOutputTemplate,
4847
IFormatProvider? formatProvider = null,
4948
bool prettyPrintJson = false,
50-
int indentSize = 4,
51-
bool useSpacesForIndent = true)
49+
int spacesPerIndent = 4)
5250
{
5351
AutoScroll = autoScroll;
5452
Theme = theme;
5553
MaxLogLines = maxLogLines;
5654
OutputTemplate = outputTemplate;
5755
FormatProvider = formatProvider ?? CultureInfo.InvariantCulture;
5856
PrettyPrintJson = prettyPrintJson;
59-
IndentSize = indentSize;
60-
UseSpacesForIndent = useSpacesForIndent;
57+
SpacesPerIndent = spacesPerIndent;
6158
}
6259

6360
public bool AutoScroll { get; set; }
@@ -81,17 +78,15 @@ public int MaxLogLines
8178

8279
public bool PrettyPrintJson { get; }
8380

84-
public int IndentSize
81+
public int SpacesPerIndent
8582
{
86-
get => _indentSize;
87-
private set => _indentSize = value switch
83+
get => _spacesPerIndent;
84+
private set => _spacesPerIndent = value switch
8885
{
8986
< 1 => 1,
9087
> 16 => 16,
9188
_ => value
9289
};
9390
}
94-
95-
public bool UseSpacesForIndent { get; }
9691
}
9792
}

0 commit comments

Comments
 (0)