Skip to content

Commit 8ebc371

Browse files
authored
Merge pull request #13 from nblumhardt/json-object-theming
Some quick fixes and tidy-up
2 parents 78362af + bea3a62 commit 8ebc371

File tree

10 files changed

+65
-43
lines changed

10 files changed

+65
-43
lines changed

src/Serilog.Sinks.Console/Sinks/SystemConsole/Formatting/ThemedDisplayValueFormatter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ value is decimal || value is byte || value is sbyte || value is short ||
184184

185185
if (value is char ch)
186186
{
187-
using (ApplyStyle(output, ConsoleThemeStyle.String, ref count))
187+
using (ApplyStyle(output, ConsoleThemeStyle.Scalar, ref count))
188188
{
189189
output.Write('\'');
190190
output.Write(ch);
@@ -194,7 +194,7 @@ value is decimal || value is byte || value is sbyte || value is short ||
194194
}
195195
}
196196

197-
using (ApplyStyle(output, ConsoleThemeStyle.Object, ref count))
197+
using (ApplyStyle(output, ConsoleThemeStyle.Scalar, ref count))
198198
scalar.Render(output, format, _formatProvider);
199199

200200
return count;

src/Serilog.Sinks.Console/Sinks/SystemConsole/Formatting/ThemedJsonValueFormatter.cs

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ protected override int VisitScalarValue(ThemedValueFormatterState state, ScalarV
4242
{
4343
if (scalar == null)
4444
throw new ArgumentNullException(nameof(scalar));
45-
return FormatLiteralValue(scalar, state.Output, state.Format);
45+
46+
// At the top level, for scalar values, use "display" rendering.
47+
if (state.IsTopLevel)
48+
return _displayFormatter.FormatLiteralValue(scalar, state.Output, state.Format);
49+
50+
return FormatLiteralValue(scalar, state.Output);
4651
}
4752

4853
protected override int VisitSequenceValue(ThemedValueFormatterState state, SequenceValue sequence)
@@ -63,7 +68,7 @@ protected override int VisitSequenceValue(ThemedValueFormatterState state, Seque
6368
state.Output.Write(delim);
6469

6570
delim = ", ";
66-
Visit(state, sequence.Elements[index]);
71+
Visit(state.Nest(), sequence.Elements[index]);
6772
}
6873

6974
using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count))
@@ -96,8 +101,9 @@ protected override int VisitStructureValue(ThemedValueFormatterState state, Stru
96101
using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count))
97102
state.Output.Write(": ");
98103

99-
count += Visit(state, property.Value);
104+
count += Visit(state.Nest(), property.Value);
100105
}
106+
101107
if (structure.TypeTag != null)
102108
{
103109
using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count))
@@ -121,7 +127,7 @@ protected override int VisitStructureValue(ThemedValueFormatterState state, Stru
121127

122128
protected override int VisitDictionaryValue(ThemedValueFormatterState state, DictionaryValue dictionary)
123129
{
124-
int count = 0;
130+
var count = 0;
125131

126132
using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count))
127133
state.Output.Write('{');
@@ -135,13 +141,19 @@ protected override int VisitDictionaryValue(ThemedValueFormatterState state, Dic
135141

136142
delim = ", ";
137143

138-
using (ApplyStyle(state.Output, ConsoleThemeStyle.String, ref count))
144+
var style = element.Key.Value == null
145+
? ConsoleThemeStyle.Null
146+
: element.Key.Value is string
147+
? ConsoleThemeStyle.String
148+
: ConsoleThemeStyle.Scalar;
149+
150+
using (ApplyStyle(state.Output, style, ref count))
139151
JsonValueFormatter.WriteQuotedJsonString((element.Key.Value ?? "null").ToString(), state.Output);
140152

141153
using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count))
142154
state.Output.Write(": ");
143155

144-
count += Visit(state, element.Value);
156+
count += Visit(state.Nest(), element.Value);
145157
}
146158

147159
using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count))
@@ -150,12 +162,8 @@ protected override int VisitDictionaryValue(ThemedValueFormatterState state, Dic
150162
return count;
151163
}
152164

153-
int FormatLiteralValue(ScalarValue scalar, TextWriter output, string format)
165+
int FormatLiteralValue(ScalarValue scalar, TextWriter output)
154166
{
155-
// At the top level, if a format string is specified, non-JSON rendering is used.
156-
if (format != null)
157-
return _displayFormatter.FormatLiteralValue(scalar, output, format);
158-
159167
var value = scalar.Value;
160168
var count = 0;
161169

@@ -175,7 +183,7 @@ int FormatLiteralValue(ScalarValue scalar, TextWriter output, string format)
175183

176184
if (value is ValueType)
177185
{
178-
if (value is int || value is uint || value is long || value is ulong || value is decimal || value is byte || (value is sbyte || value is short) || value is ushort)
186+
if (value is int || value is uint || value is long || value is ulong || value is decimal || value is byte || value is sbyte || value is short || value is ushort)
179187
{
180188
using (ApplyStyle(output, ConsoleThemeStyle.Number, ref count))
181189
output.Write(((IFormattable)value).ToString(null, CultureInfo.InvariantCulture));
@@ -184,23 +192,25 @@ int FormatLiteralValue(ScalarValue scalar, TextWriter output, string format)
184192

185193
if (value is double d)
186194
{
187-
if (double.IsNaN(d) || double.IsInfinity(d))
188-
using (ApplyStyle(output, ConsoleThemeStyle.String, ref count))
195+
using (ApplyStyle(output, ConsoleThemeStyle.Number, ref count))
196+
{
197+
if (double.IsNaN(d) || double.IsInfinity(d))
189198
JsonValueFormatter.WriteQuotedJsonString(d.ToString(CultureInfo.InvariantCulture), output);
190-
else
191-
using (ApplyStyle(output, ConsoleThemeStyle.Number, ref count))
199+
else
192200
output.Write(d.ToString("R", CultureInfo.InvariantCulture));
201+
}
193202
return count;
194203
}
195204

196205
if (value is float f)
197206
{
198-
if (double.IsNaN(f) || double.IsInfinity(f))
199-
using (ApplyStyle(output, ConsoleThemeStyle.String, ref count))
207+
using (ApplyStyle(output, ConsoleThemeStyle.Number, ref count))
208+
{
209+
if (double.IsNaN(f) || double.IsInfinity(f))
200210
JsonValueFormatter.WriteQuotedJsonString(f.ToString(CultureInfo.InvariantCulture), output);
201-
else
202-
using (ApplyStyle(output, ConsoleThemeStyle.Number, ref count))
211+
else
203212
output.Write(f.ToString("R", CultureInfo.InvariantCulture));
213+
}
204214
return count;
205215
}
206216

@@ -214,14 +224,14 @@ int FormatLiteralValue(ScalarValue scalar, TextWriter output, string format)
214224

215225
if (value is char ch)
216226
{
217-
using (ApplyStyle(output, ConsoleThemeStyle.String, ref count))
227+
using (ApplyStyle(output, ConsoleThemeStyle.Scalar, ref count))
218228
JsonValueFormatter.WriteQuotedJsonString(ch.ToString(), output);
219229
return count;
220230
}
221231

222232
if (value is DateTime || value is DateTimeOffset)
223233
{
224-
using (ApplyStyle(output, ConsoleThemeStyle.String, ref count))
234+
using (ApplyStyle(output, ConsoleThemeStyle.Scalar, ref count))
225235
{
226236
output.Write('"');
227237
output.Write(((IFormattable)value).ToString("O", CultureInfo.InvariantCulture));
@@ -231,7 +241,7 @@ int FormatLiteralValue(ScalarValue scalar, TextWriter output, string format)
231241
}
232242
}
233243

234-
using (ApplyStyle(output, ConsoleThemeStyle.String, ref count))
244+
using (ApplyStyle(output, ConsoleThemeStyle.Scalar, ref count))
235245
JsonValueFormatter.WriteQuotedJsonString(value.ToString(), output);
236246

237247
return count;

src/Serilog.Sinks.Console/Sinks/SystemConsole/Formatting/ThemedValueFormatter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ protected StyleReset ApplyStyle(TextWriter output, ConsoleThemeStyle style, ref
3434
return _theme.Apply(output, style, ref invisibleCharacterCount);
3535
}
3636

37-
public int Format(LogEventPropertyValue value, TextWriter output, string format)
37+
public int Format(LogEventPropertyValue value, TextWriter output, string format, bool literalTopLevel = false)
3838
{
39-
return Visit(new ThemedValueFormatterState { Output = output, Format = format }, value);
39+
return Visit(new ThemedValueFormatterState { Output = output, Format = format, IsTopLevel = literalTopLevel }, value);
4040
}
4141

4242
public abstract ThemedValueFormatter SwitchTheme(ConsoleTheme theme);

src/Serilog.Sinks.Console/Sinks/SystemConsole/Formatting/ThemedValueFormatterState.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ struct ThemedValueFormatterState
2020
{
2121
public TextWriter Output;
2222
public string Format;
23+
public bool IsTopLevel;
2324

2425
public ThemedValueFormatterState Nest()
2526
{

src/Serilog.Sinks.Console/Sinks/SystemConsole/Rendering/ThemedMessageTemplateRenderer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,15 @@ int RenderAlignedPropertyTokenUnbuffered(PropertyToken pt, TextWriter output, Lo
126126

127127
int RenderValue(ConsoleTheme theme, ThemedValueFormatter valueFormatter, LogEventPropertyValue propertyValue, TextWriter output, string format)
128128
{
129-
if (_isLiteral && propertyValue is ScalarValue sv && (sv.Value is string str || sv.Value is char ch))
129+
if (_isLiteral && propertyValue is ScalarValue sv && sv.Value is string)
130130
{
131131
var count = 0;
132132
using (theme.Apply(output, ConsoleThemeStyle.String, ref count))
133133
output.Write(sv.Value);
134134
return count;
135135
}
136136

137-
return valueFormatter.Format(propertyValue, output, format);
137+
return valueFormatter.Format(propertyValue, output, format, _isLiteral);
138138
}
139139
}
140140
}

src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/AnsiConsoleThemes.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static class AnsiConsoleThemes
3030
[ConsoleThemeStyle.String] = "\x1b[36;1m",
3131
[ConsoleThemeStyle.Number] = "\x1b[35;1m",
3232
[ConsoleThemeStyle.Boolean] = "\x1b[34;1m",
33-
[ConsoleThemeStyle.Object] = "\x1b[32;1m",
33+
[ConsoleThemeStyle.Scalar] = "\x1b[32;1m",
3434
[ConsoleThemeStyle.LevelVerbose] = "\x1b[37m",
3535
[ConsoleThemeStyle.LevelDebug] = "\x1b[37m",
3636
[ConsoleThemeStyle.LevelInformation] = "\x1b[37;1m",
@@ -51,7 +51,7 @@ static class AnsiConsoleThemes
5151
[ConsoleThemeStyle.String] = "\x1b[1m\x1b[37;1m",
5252
[ConsoleThemeStyle.Number] = "\x1b[1m\x1b[37;1m",
5353
[ConsoleThemeStyle.Boolean] = "\x1b[1m\x1b[37;1m",
54-
[ConsoleThemeStyle.Object] = "\x1b[1m\x1b[37;1m",
54+
[ConsoleThemeStyle.Scalar] = "\x1b[1m\x1b[37;1m",
5555
[ConsoleThemeStyle.LevelVerbose] = "\x1b[30;1m",
5656
[ConsoleThemeStyle.LevelDebug] = "\x1b[30;1m",
5757
[ConsoleThemeStyle.LevelInformation] ="\x1b[37;1m",
@@ -72,7 +72,7 @@ static class AnsiConsoleThemes
7272
[ConsoleThemeStyle.String] = "\x1b[38;5;0216m",
7373
[ConsoleThemeStyle.Number] = "\x1b[38;5;151m",
7474
[ConsoleThemeStyle.Boolean] = "\x1b[38;5;0038m",
75-
[ConsoleThemeStyle.Object] = "\x1b[38;5;0079m",
75+
[ConsoleThemeStyle.Scalar] = "\x1b[38;5;0079m",
7676
[ConsoleThemeStyle.LevelVerbose] = "\x1b[37m",
7777
[ConsoleThemeStyle.LevelDebug] = "\x1b[37m",
7878
[ConsoleThemeStyle.LevelInformation] = "\x1b[37;1m",

src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/ConsoleThemeStyle.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
using System;
16+
using System.ComponentModel;
17+
1518
namespace Serilog.Sinks.SystemConsole.Themes
1619
{
1720
/// <summary>
@@ -66,10 +69,16 @@ public enum ConsoleThemeStyle
6669
/// </summary>
6770
Boolean,
6871

72+
/// <summary>
73+
/// All other scalar values, e.g. <see cref="System.Guid"/> instances.
74+
/// </summary>
75+
Scalar,
76+
6977
/// <summary>
7078
/// Unrecogized literal values, e.g. <see cref="System.Guid"/> instances.
7179
/// </summary>
72-
Object,
80+
[Obsolete("Use ConsoleThemeStyle.Scalar instead"), EditorBrowsable(EditorBrowsableState.Never)]
81+
Object = Scalar,
7382

7483
/// <summary>
7584
/// Level indicator.

src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/SystemConsoleThemes.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static class SystemConsoleThemes
3131
[ConsoleThemeStyle.String] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.Cyan },
3232
[ConsoleThemeStyle.Number] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.Magenta },
3333
[ConsoleThemeStyle.Boolean] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.Blue },
34-
[ConsoleThemeStyle.Object] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.Green },
34+
[ConsoleThemeStyle.Scalar] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.Green },
3535
[ConsoleThemeStyle.LevelVerbose] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.Gray },
3636
[ConsoleThemeStyle.LevelDebug] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.Gray },
3737
[ConsoleThemeStyle.LevelInformation] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.White },
@@ -52,7 +52,7 @@ static class SystemConsoleThemes
5252
[ConsoleThemeStyle.String] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.White },
5353
[ConsoleThemeStyle.Number] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.White },
5454
[ConsoleThemeStyle.Boolean] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.White },
55-
[ConsoleThemeStyle.Object] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.White },
55+
[ConsoleThemeStyle.Scalar] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.White },
5656
[ConsoleThemeStyle.LevelVerbose] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.DarkGray },
5757
[ConsoleThemeStyle.LevelDebug] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.DarkGray },
5858
[ConsoleThemeStyle.LevelInformation] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.White },

test/Serilog.Sinks.Console.Tests/Formatting/ThemedJsonValueFormatterTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ public TestThemedJsonValueFormatter()
2020
public string Format(object literal)
2121
{
2222
var output = new StringWriter();
23-
Format(new ScalarValue(literal), output, null);
24-
return output.ToString();
23+
Format(new SequenceValue(new [] {new ScalarValue(literal)}), output, null);
24+
var o = output.ToString();
25+
return o.Substring(1, o.Length - 2);
2526
}
2627
}
2728

test/Serilog.Sinks.Console.Tests/Rendering/ThemedMessageTemplateRendererTests.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
using System.Linq;
55
using System.Text;
66
using Xunit;
7-
using Serilog.Core;
87
using Serilog.Sinks.SystemConsole.Themes;
98
using Serilog.Sinks.SystemConsole.Formatting;
109
using Serilog.Sinks.SystemConsole.Rendering;
11-
using MessageTemplateParser = Serilog.Parsing.MessageTemplateParser;
1210

1311
namespace Serilog.Sinks.Console.Tests.Rendering
1412
{
@@ -17,8 +15,9 @@ public class ThemedMessageTemplateRendererTests
1715
class Chair
1816
{
1917
// ReSharper disable UnusedMember.Local
20-
public string Back { get { return "straight"; } }
21-
public int[] Legs { get { return new[] { 1, 2, 3, 4 }; } }
18+
public string Back => "straight";
19+
20+
public int[] Legs => new[] { 1, 2, 3, 4 };
2221
// ReSharper restore UnusedMember.Local
2322
public override string ToString()
2423
{
@@ -29,9 +28,11 @@ public override string ToString()
2928
class Receipt
3029
{
3130
// ReSharper disable UnusedMember.Local
32-
public decimal Sum { get { return 12.345m; } }
33-
public DateTime When { get { return new DateTime(2013, 5, 20, 16, 39, 0); } }
31+
public decimal Sum => 12.345m;
32+
33+
public DateTime When => new DateTime(2013, 5, 20, 16, 39, 0);
3434
// ReSharper restore UnusedMember.Local
35+
3536
public override string ToString()
3637
{
3738
return "a receipt";

0 commit comments

Comments
 (0)