Skip to content

Commit 6195c6f

Browse files
committed
Fixed inspection issues
1 parent d4a10f4 commit 6195c6f

File tree

15 files changed

+132
-205
lines changed

15 files changed

+132
-205
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private void InitializeComponent()
3434
{
3535
this.richTextBox1.BackColor = System.Drawing.SystemColors.Window;
3636
this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
37-
this.richTextBox1.Font = new System.Drawing.Font("Cascadia Mono", 10.8F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point);
37+
this.richTextBox1.Font = new System.Drawing.Font("Cascadia Mono", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
3838
this.richTextBox1.Location = new System.Drawing.Point(0, 0);
3939
this.richTextBox1.Name = "richTextBox1";
4040
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void ScalarTypes_DefaultFormatting_RendersCorrectly(object? scalarValue,
2626
var prop = new LogEventProperty("Scalar", new ScalarValue(scalarValue));
2727
var logEvent = new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null, _parser.Parse("{Scalar}"), new[] { prop });
2828

29-
string actual = RenderAndGetText(logEvent, "{Message}", CultureInfo.InvariantCulture);
29+
var actual = RenderAndGetText(logEvent, "{Message}", CultureInfo.InvariantCulture);
3030

3131
if (scalarValue is float || scalarValue is double || scalarValue is decimal)
3232
{
@@ -60,7 +60,7 @@ public void ScalarTypes_JsonFormatting_RendersCorrectly(object? scalarValue, str
6060
var prop = new LogEventProperty("Scalar", new ScalarValue(scalarValue));
6161
var logEvent = new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null, _parser.Parse("{Scalar}"), new[] { prop });
6262

63-
string actual = RenderAndGetText(logEvent, "{Message:j}", CultureInfo.InvariantCulture);
63+
var actual = RenderAndGetText(logEvent, "{Message:j}", CultureInfo.InvariantCulture);
6464

6565
if (scalarValue is decimal decVal)
6666
{
@@ -101,7 +101,7 @@ public void DateTime_WithUSCulture_RendersCorrectly()
101101
var prop = new LogEventProperty("DateTime", new ScalarValue(dateTime));
102102
var logEvent = new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null, _parser.Parse("{DateTime}"), new[] { prop });
103103

104-
string actual = RenderAndGetText(logEvent, "{Message}", new CultureInfo("en-US"));
104+
var actual = RenderAndGetText(logEvent, "{Message}", new CultureInfo("en-US"));
105105
Assert.Equal("6/6/2025 12:09:55 PM", actual);
106106
}
107107

@@ -112,7 +112,7 @@ public void DateTime_WithGermanCulture_RendersCorrectly()
112112
var prop = new LogEventProperty("DateTime", new ScalarValue(dateTime));
113113
var logEvent = new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null, _parser.Parse("{DateTime}"), new[] { prop });
114114

115-
string actual = RenderAndGetText(logEvent, "{Message}", new CultureInfo("de-DE"));
115+
var actual = RenderAndGetText(logEvent, "{Message}", new CultureInfo("de-DE"));
116116
Assert.Equal("06.06.2025 12:09:55", actual);
117117
}
118118

@@ -123,7 +123,7 @@ public void DateTimeOffset_WithUSCulture_RendersCorrectly()
123123
var prop = new LogEventProperty("DateTimeOffset", new ScalarValue(dateTimeOffset));
124124
var logEvent = new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null, _parser.Parse("{DateTimeOffset}"), new[] { prop });
125125

126-
string actual = RenderAndGetText(logEvent, "{Message}", new CultureInfo("en-US"));
126+
var actual = RenderAndGetText(logEvent, "{Message}", new CultureInfo("en-US"));
127127
Assert.Equal("6/6/2025 12:09:55 PM +02:00", actual);
128128
}
129129

@@ -134,7 +134,7 @@ public void DateTimeOffset_WithGermanCulture_RendersCorrectly()
134134
var prop = new LogEventProperty("DateTimeOffset", new ScalarValue(dateTimeOffset));
135135
var logEvent = new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null, _parser.Parse("{DateTimeOffset}"), new[] { prop });
136136

137-
string actual = RenderAndGetText(logEvent, "{Message}", new CultureInfo("de-DE"));
137+
var actual = RenderAndGetText(logEvent, "{Message}", new CultureInfo("de-DE"));
138138
Assert.Equal("06.06.2025 12:09:55 +02:00", actual);
139139
}
140140
}

Serilog.Sinks.RichTextBox.WinForms.Colored/Sinks/RichTextBoxForms/Collections/ConcurrentCircularBuffer.cs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,10 @@
22

33
namespace Serilog.Sinks.RichTextBoxForms.Collections
44
{
5-
/// <summary>
6-
/// Thread-safe circular buffer that overwrites the oldest element when capacity is exceeded.
7-
/// Optimized for the pattern «many producers / single consumer snapshot». Add() never blocks; the
8-
/// internal array size is fixed (<see cref="Capacity"/>). Snapshot() returns a snapshot of the
9-
/// current content in chronological order (oldest → newest).
10-
/// </summary>
115
internal sealed class ConcurrentCircularBuffer<T>
126
{
137
private readonly T[] _buffer;
148
private readonly int _capacity;
15-
16-
// _head points to the index of the oldest entry. _count is the number of valid items.
179
private int _head;
1810
private int _count;
1911

@@ -27,22 +19,6 @@ public ConcurrentCircularBuffer(int capacity)
2719
_count = 0;
2820
}
2921

30-
public int Capacity => _capacity;
31-
32-
public int Count
33-
{
34-
get
35-
{
36-
lock (_sync)
37-
{
38-
return _count;
39-
}
40-
}
41-
}
42-
43-
/// <summary>
44-
/// Adds an item to the buffer, overwriting the oldest element when the buffer is full.
45-
/// </summary>
4622
public void Add(T item)
4723
{
4824
lock (_sync)
@@ -61,10 +37,6 @@ public void Add(T item)
6137
}
6238
}
6339

64-
/// <summary>
65-
/// Fills the provided list with a snapshot of the current content in chronological order.
66-
/// The list is cleared before being filled.
67-
/// </summary>
6840
public void TakeSnapshot(List<T> target)
6941
{
7042
lock (_sync)

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

Lines changed: 30 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -39,38 +39,29 @@ public void FormatLiteralValue(ScalarValue scalar, IRtfCanvas canvas, string? fo
3939
{
4040
var value = scalar.Value;
4141

42-
if (value is null)
43-
{
44-
Theme.Render(canvas, StyleToken.Null, "null");
45-
return;
46-
}
47-
if (value is string text)
48-
{
49-
RenderString(text, canvas, format, isLiteral);
50-
return;
51-
}
52-
if (value is byte[] bytes)
53-
{
54-
Theme.Render(canvas, StyleToken.String, $"\"{Convert.ToBase64String(bytes)}\"");
55-
return;
56-
}
57-
if (value is bool b)
58-
{
59-
Theme.Render(canvas, StyleToken.Boolean, b.ToString());
60-
return;
61-
}
62-
if (value is Uri uri)
63-
{
64-
Theme.Render(canvas, StyleToken.Scalar, uri.ToString());
65-
return;
66-
}
67-
if (value is IFormattable formattable)
68-
{
69-
RenderFormattable(formattable, format, canvas);
70-
return;
71-
}
72-
73-
var sb = StringBuilderCache.Acquire(256);
42+
switch (value)
43+
{
44+
case null:
45+
Theme.Render(canvas, StyleToken.Null, "null");
46+
return;
47+
case string text:
48+
RenderString(text, canvas, format, isLiteral);
49+
return;
50+
case byte[] bytes:
51+
Theme.Render(canvas, StyleToken.String, $"\"{Convert.ToBase64String(bytes)}\"");
52+
return;
53+
case bool b:
54+
Theme.Render(canvas, StyleToken.Boolean, b.ToString());
55+
return;
56+
case Uri uri:
57+
Theme.Render(canvas, StyleToken.Scalar, uri.ToString());
58+
return;
59+
case IFormattable formattable:
60+
RenderFormattable(formattable, format, canvas);
61+
return;
62+
}
63+
64+
var sb = StringBuilderCache.Acquire();
7465

7566
using (var writer = new StringWriter(sb))
7667
{
@@ -82,28 +73,21 @@ public void FormatLiteralValue(ScalarValue scalar, IRtfCanvas canvas, string? fo
8273

8374
private void RenderString(string text, IRtfCanvas canvas, string? format, bool isLiteral)
8475
{
85-
bool effectivelyLiteral = isLiteral || (format != null && format.Contains("l"));
86-
if (effectivelyLiteral)
87-
{
88-
Theme.Render(canvas, StyleToken.String, text);
89-
}
90-
else
91-
{
92-
Theme.Render(canvas, StyleToken.String, $"\"{text.Replace("\"", "\\\"")}\"");
93-
}
76+
var effectivelyLiteral = isLiteral || (format != null && format.Contains("l"));
77+
Theme.Render(canvas, StyleToken.String, effectivelyLiteral ? text : $"\"{text.Replace("\"", "\\\"")}\"");
9478
}
9579

9680
private void RenderFormattable(IFormattable formattable, string? format, IRtfCanvas canvas)
9781
{
9882
// Use Number style for numbers, Scalar for others (DateTime, Guid, etc.)
9983
var type = formattable.GetType();
100-
StyleToken token =
84+
var token =
10185
type == typeof(float) || type == typeof(double) || type == typeof(decimal) ||
10286
type == typeof(int) || type == typeof(uint) || type == typeof(long) || type == typeof(ulong) ||
10387
type == typeof(byte) || type == typeof(sbyte) || type == typeof(short) || type == typeof(ushort)
10488
? StyleToken.Number : StyleToken.Scalar;
10589

106-
string? effectiveFormat = format;
90+
var effectiveFormat = format;
10791

10892
// Remove sink-specific format specifiers (e.g. "l" for literal, "j" for JSON) that are not
10993
// recognised by the underlying IFormattable implementation and would otherwise trigger
@@ -140,7 +124,7 @@ private void RenderFormattable(IFormattable formattable, string? format, IRtfCan
140124

141125
protected override bool VisitDictionaryValue(ValueFormatterState state, DictionaryValue dictionary)
142126
{
143-
if (state.Format != null && state.Format.Contains("j"))
127+
if (state.Format.Contains("j"))
144128
{
145129
_jsonValueFormatter ??= new JsonValueFormatter(Theme, _formatProvider);
146130
_jsonValueFormatter.Format(dictionary, state.Canvas, state.Format, state.IsLiteral);
@@ -177,7 +161,7 @@ protected override bool VisitScalarValue(ValueFormatterState state, ScalarValue
177161

178162
protected override bool VisitSequenceValue(ValueFormatterState state, SequenceValue sequence)
179163
{
180-
if (state.Format != null && state.Format.Contains("j"))
164+
if (state.Format.Contains("j"))
181165
{
182166
_jsonValueFormatter ??= new JsonValueFormatter(Theme, _formatProvider);
183167
_jsonValueFormatter.Format(sequence, state.Canvas, state.Format, state.IsLiteral);
@@ -204,7 +188,7 @@ protected override bool VisitSequenceValue(ValueFormatterState state, SequenceVa
204188

205189
protected override bool VisitStructureValue(ValueFormatterState state, StructureValue structure)
206190
{
207-
if (state.Format != null && state.Format.Contains("j"))
191+
if (state.Format.Contains("j"))
208192
{
209193
_jsonValueFormatter ??= new JsonValueFormatter(Theme, _formatProvider);
210194
_jsonValueFormatter.Format(structure, state.Canvas, state.Format, state.IsLiteral);

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

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,18 @@ private void FormatLiteralValue(ScalarValue scalar, IRtfCanvas canvas)
172172

173173
using (var writer = new StringWriter(sb))
174174
{
175-
// For dates in JSON, always use ISO 8601 format (O)
176-
if (value is DateTime dt)
175+
switch (value)
177176
{
178-
writer.Write(dt.ToString("O", CultureInfo.InvariantCulture));
179-
}
180-
else if (value is DateTimeOffset dto)
181-
{
182-
writer.Write(dto.ToString("O", CultureInfo.InvariantCulture));
183-
}
184-
else
185-
{
186-
scalar.Render(writer, null, _formatProvider);
177+
// For dates in JSON, always use ISO 8601 format (O)
178+
case DateTime dt:
179+
writer.Write(dt.ToString("O", CultureInfo.InvariantCulture));
180+
break;
181+
case DateTimeOffset dto:
182+
writer.Write(dto.ToString("O", CultureInfo.InvariantCulture));
183+
break;
184+
default:
185+
scalar.Render(writer, null, _formatProvider);
186+
break;
187187
}
188188
}
189189

@@ -199,7 +199,7 @@ private void FormatLiteralValue(ScalarValue scalar, IRtfCanvas canvas)
199199
return;
200200
}
201201

202-
var sb = StringBuilderCache.Acquire(256);
202+
var sb = StringBuilderCache.Acquire();
203203

204204
using (var writer = new StringWriter(sb))
205205
{
@@ -217,13 +217,13 @@ private void RenderFormattable(IRtfCanvas canvas, IFormattable formattable, stri
217217
{
218218
// Use Number style for numbers, Scalar for others (DateTime, Guid, etc.)
219219
var type = formattable.GetType();
220-
StyleToken token =
220+
var token =
221221
type == typeof(float) || type == typeof(double) || type == typeof(decimal) ||
222222
type == typeof(int) || type == typeof(uint) || type == typeof(long) || type == typeof(ulong) ||
223223
type == typeof(byte) || type == typeof(sbyte) || type == typeof(short) || type == typeof(ushort)
224224
? StyleToken.Number : StyleToken.Scalar;
225225

226-
string? effectiveFormat = format;
226+
var effectiveFormat = format;
227227

228228
// Remove sink-specific format specifiers (e.g. "l" for literal, "j" for JSON) that are not
229229
// recognised by the underlying IFormattable implementation and would otherwise trigger
@@ -258,20 +258,19 @@ private void RenderFormattable(IRtfCanvas canvas, IFormattable formattable, stri
258258
Theme.Render(canvas, token, renderedValue);
259259
}
260260

261-
public static void WriteQuotedJsonString(string str, TextWriter output)
261+
private static void WriteQuotedJsonString(string str, TextWriter output)
262262
{
263263
output.Write('\"');
264264

265-
for (var i = 0; i < str.Length; ++i)
265+
foreach (var c in str)
266266
{
267-
var c = str[i];
268267
switch (c)
269268
{
270269
case '"':
271270
output.Write("\\\"");
272271
break;
273272
case '\\':
274-
output.Write("\\\\");
273+
output.Write(@"\\");
275274
break;
276275
case '\n':
277276
output.Write("\\n");
@@ -297,7 +296,7 @@ public static void WriteQuotedJsonString(string str, TextWriter output)
297296
output.Write('\"');
298297
}
299298

300-
public static string GetQuotedJsonString(string str)
299+
private static string GetQuotedJsonString(string str)
301300
{
302301
var sb = StringBuilderCache.Acquire(str.Length + 2);
303302
using (var writer = new StringWriter(sb))

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ protected ValueFormatter(Theme theme)
3030
Theme = theme;
3131
}
3232

33-
public Theme Theme { get; set; }
33+
protected Theme Theme { get; }
3434

3535
public void Format(LogEventPropertyValue value, IRtfCanvas canvas, string format, bool isLiteral)
3636
{
37-
Visit(new ValueFormatterState(canvas, format, true, isLiteral), value);
37+
Visit(new ValueFormatterState(canvas, format, isLiteral), value);
3838
}
3939
}
4040
}

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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,20 @@ namespace Serilog.Sinks.RichTextBoxForms.Formatting
2222
{
2323
public readonly struct ValueFormatterState
2424
{
25-
public ValueFormatterState(IRtfCanvas canvas)
26-
{
27-
Canvas = canvas;
28-
Format = string.Empty;
29-
IsTopLevel = false;
30-
IsLiteral = false;
31-
}
32-
33-
public ValueFormatterState(IRtfCanvas canvas, string format, bool isTopLevel, bool isLiteral)
25+
public ValueFormatterState(IRtfCanvas canvas, string format, bool isLiteral)
3426
{
3527
Canvas = canvas;
3628
Format = format;
37-
IsTopLevel = isTopLevel;
3829
IsLiteral = isLiteral;
3930
}
4031

4132
public string Format { get; }
42-
public bool IsTopLevel { get; }
4333
public bool IsLiteral { get; }
4434
public IRtfCanvas Canvas { get; }
4535

4636
public ValueFormatterState Next(string? format = null)
4737
{
48-
return new ValueFormatterState(Canvas, format ?? Format, false, IsLiteral);
38+
return new ValueFormatterState(Canvas, format ?? Format, IsLiteral);
4939
}
5040
}
5141
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ namespace Serilog.Sinks.RichTextBoxForms.Rendering
2828
{
2929
public class EventPropertyTokenRenderer : ITokenRenderer
3030
{
31-
/// Recommended initial buffer size for formatting non-string property values.
32-
private const int InitialBufferSize = 256;
33-
3431
private readonly IFormatProvider? _formatProvider;
3532
private readonly Theme _theme;
3633
private readonly PropertyToken _token;
@@ -56,7 +53,7 @@ public void Render(LogEvent logEvent, IRtfCanvas canvas)
5653
}
5754
else
5855
{
59-
var sb = StringBuilderCache.Acquire(InitialBufferSize);
56+
var sb = StringBuilderCache.Acquire();
6057

6158
using (var writer = new StringWriter(sb))
6259
{

0 commit comments

Comments
 (0)