Skip to content

Commit 9a1db27

Browse files
committed
Add configurable JSON indent size to demo UI
Introduces a numeric up/down control in the demo form to allow users to set the number of spaces per indentation level for pretty-printed JSON (0-16, default 2). Updates the logger and sink configuration to use the selected indent size. Also changes the default indent from 4 to 2 in the sink options and extension method.
1 parent ec5845e commit 9a1db27

File tree

4 files changed

+85
-10
lines changed

4 files changed

+85
-10
lines changed

Demo/Form1.Designer.cs

Lines changed: 45 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Demo/Form1.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public partial class Form1 : Form
3636
private RichTextBoxSink? _sink;
3737
private bool _toolbarsVisible = true;
3838
private bool _prettyPrintJson = false;
39+
private int _spacesPerIndent = 2;
40+
private bool _updatingIndent = false;
3941

4042
public Form1()
4143
{
@@ -49,7 +51,8 @@ private void Initialize()
4951
theme: ThemePresets.Literate,
5052
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:l}{NewLine}{Exception}",
5153
formatProvider: new CultureInfo("en-US"),
52-
prettyPrintJson: _prettyPrintJson);
54+
prettyPrintJson: _prettyPrintJson,
55+
spacesPerIndent: _spacesPerIndent);
5356

5457
_sink = new RichTextBoxSink(richTextBox1, _options);
5558
Log.Logger = new LoggerConfiguration()
@@ -80,6 +83,14 @@ private void Initialize()
8083
Log.Debug("Started logger.");
8184
btnDispose.Enabled = true;
8285
btnPrettyPrint.Text = _prettyPrintJson ? "Disable Pretty Print" : "Enable Pretty Print";
86+
87+
// Update the numeric up/down control without triggering events
88+
_updatingIndent = true;
89+
if (numericUpDownSpacesPerIndent.Value != _spacesPerIndent)
90+
{
91+
numericUpDownSpacesPerIndent.Value = _spacesPerIndent;
92+
}
93+
_updatingIndent = false;
8394
}
8495

8596
private void Form1_Load(object sender, EventArgs e)
@@ -365,6 +376,29 @@ private void btnPrettyPrint_Click(object sender, EventArgs e)
365376
Log.Information("Pretty print JSON: {PrettyPrint}", _prettyPrintJson);
366377
}
367378

379+
private void numericUpDownSpacesPerIndent_ValueChanged(object sender, EventArgs e)
380+
{
381+
// Prevent recursive calls when we're updating the value programmatically
382+
if (_updatingIndent)
383+
{
384+
return;
385+
}
386+
387+
var newValue = (int)numericUpDownSpacesPerIndent.Value;
388+
if (newValue == _spacesPerIndent)
389+
{
390+
return;
391+
}
392+
393+
_spacesPerIndent = newValue;
394+
395+
// Recreate the sink and logger with new indent size
396+
CloseAndFlush();
397+
Initialize();
398+
399+
Log.Information("Spaces per indent changed to: {SpacesPerIndent}", _spacesPerIndent);
400+
}
401+
368402
private void Form1_KeyDown(object sender, KeyEventArgs e)
369403
{
370404
if (e.Control && e.KeyCode == Keys.T)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +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="spacesPerIndent">Number of spaces per indentation level when pretty printing JSON. Defaults to 4. Must be between 1 and 16.</param>
49+
/// <param name="spacesPerIndent">Number of spaces per indentation level when pretty printing JSON. Defaults to 2.</param>
5050
/// <returns>The logger configuration, for chaining.</returns>
5151
public static LoggerConfiguration RichTextBox(
5252
this LoggerSinkConfiguration sinkConfiguration,
@@ -60,7 +60,7 @@ public static LoggerConfiguration RichTextBox(
6060
LogEventLevel minimumLogEventLevel = LogEventLevel.Verbose,
6161
LoggingLevelSwitch? levelSwitch = null,
6262
bool prettyPrintJson = false,
63-
int spacesPerIndent = 4)
63+
int spacesPerIndent = 2)
6464
{
6565
var appliedTheme = theme ?? ThemePresets.Literate;
6666
var appliedFormatProvider = formatProvider ?? CultureInfo.InvariantCulture;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ 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="spacesPerIndent">Number of spaces per indentation level when pretty printing JSON. Defaults to 4. Must be between 1 and 16.</param>
41+
/// <param name="spacesPerIndent">Number of spaces per indentation level when pretty printing JSON. Defaults to 2.</param>
4242
public RichTextBoxSinkOptions(
4343
Theme theme,
4444
bool autoScroll = true,
4545
int maxLogLines = 256,
4646
string outputTemplate = DefaultOutputTemplate,
4747
IFormatProvider? formatProvider = null,
4848
bool prettyPrintJson = false,
49-
int spacesPerIndent = 4)
49+
int spacesPerIndent = 2)
5050
{
5151
AutoScroll = autoScroll;
5252
Theme = theme;
@@ -83,7 +83,7 @@ public int SpacesPerIndent
8383
get => _spacesPerIndent;
8484
private set => _spacesPerIndent = value switch
8585
{
86-
< 1 => 1,
86+
< 0 => 0,
8787
> 16 => 16,
8888
_ => value
8989
};

0 commit comments

Comments
 (0)