Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ value is decimal || value is byte || value is sbyte || value is short ||

if (value is char ch)
{
using (ApplyStyle(output, ConsoleThemeStyle.String, ref count))
using (ApplyStyle(output, ConsoleThemeStyle.Scalar, ref count))
{
output.Write('\'');
output.Write(ch);
Expand All @@ -194,7 +194,7 @@ value is decimal || value is byte || value is sbyte || value is short ||
}
}

using (ApplyStyle(output, ConsoleThemeStyle.Object, ref count))
using (ApplyStyle(output, ConsoleThemeStyle.Scalar, ref count))
scalar.Render(output, format, _formatProvider);

return count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ protected override int VisitScalarValue(ThemedValueFormatterState state, ScalarV
{
if (scalar == null)
throw new ArgumentNullException(nameof(scalar));
return FormatLiteralValue(scalar, state.Output, state.Format);

// At the top level, for scalar values, use "display" rendering.
if (state.IsTopLevel)
return _displayFormatter.FormatLiteralValue(scalar, state.Output, state.Format);

return FormatLiteralValue(scalar, state.Output);
}

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

delim = ", ";
Visit(state, sequence.Elements[index]);
Visit(state.Nest(), sequence.Elements[index]);
}

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

count += Visit(state, property.Value);
count += Visit(state.Nest(), property.Value);
}

if (structure.TypeTag != null)
{
using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count))
Expand All @@ -121,7 +127,7 @@ protected override int VisitStructureValue(ThemedValueFormatterState state, Stru

protected override int VisitDictionaryValue(ThemedValueFormatterState state, DictionaryValue dictionary)
{
int count = 0;
var count = 0;

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

delim = ", ";

using (ApplyStyle(state.Output, ConsoleThemeStyle.String, ref count))
var style = element.Key.Value == null
? ConsoleThemeStyle.Null
: element.Key.Value is string
? ConsoleThemeStyle.String
: ConsoleThemeStyle.Scalar;

using (ApplyStyle(state.Output, style, ref count))
JsonValueFormatter.WriteQuotedJsonString((element.Key.Value ?? "null").ToString(), state.Output);

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

count += Visit(state, element.Value);
count += Visit(state.Nest(), element.Value);
}

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

int FormatLiteralValue(ScalarValue scalar, TextWriter output, string format)
int FormatLiteralValue(ScalarValue scalar, TextWriter output)
{
// At the top level, if a format string is specified, non-JSON rendering is used.
if (format != null)
return _displayFormatter.FormatLiteralValue(scalar, output, format);

var value = scalar.Value;
var count = 0;

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

if (value is ValueType)
{
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)
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)
{
using (ApplyStyle(output, ConsoleThemeStyle.Number, ref count))
output.Write(((IFormattable)value).ToString(null, CultureInfo.InvariantCulture));
Expand All @@ -184,23 +192,25 @@ int FormatLiteralValue(ScalarValue scalar, TextWriter output, string format)

if (value is double d)
{
if (double.IsNaN(d) || double.IsInfinity(d))
using (ApplyStyle(output, ConsoleThemeStyle.String, ref count))
using (ApplyStyle(output, ConsoleThemeStyle.Number, ref count))
{
if (double.IsNaN(d) || double.IsInfinity(d))
JsonValueFormatter.WriteQuotedJsonString(d.ToString(CultureInfo.InvariantCulture), output);
else
using (ApplyStyle(output, ConsoleThemeStyle.Number, ref count))
else
output.Write(d.ToString("R", CultureInfo.InvariantCulture));
}
return count;
}

if (value is float f)
{
if (double.IsNaN(f) || double.IsInfinity(f))
using (ApplyStyle(output, ConsoleThemeStyle.String, ref count))
using (ApplyStyle(output, ConsoleThemeStyle.Number, ref count))
{
if (double.IsNaN(f) || double.IsInfinity(f))
JsonValueFormatter.WriteQuotedJsonString(f.ToString(CultureInfo.InvariantCulture), output);
else
using (ApplyStyle(output, ConsoleThemeStyle.Number, ref count))
else
output.Write(f.ToString("R", CultureInfo.InvariantCulture));
}
return count;
}

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

if (value is char ch)
{
using (ApplyStyle(output, ConsoleThemeStyle.String, ref count))
using (ApplyStyle(output, ConsoleThemeStyle.Scalar, ref count))
JsonValueFormatter.WriteQuotedJsonString(ch.ToString(), output);
return count;
}

if (value is DateTime || value is DateTimeOffset)
{
using (ApplyStyle(output, ConsoleThemeStyle.String, ref count))
using (ApplyStyle(output, ConsoleThemeStyle.Scalar, ref count))
{
output.Write('"');
output.Write(((IFormattable)value).ToString("O", CultureInfo.InvariantCulture));
Expand All @@ -231,7 +241,7 @@ int FormatLiteralValue(ScalarValue scalar, TextWriter output, string format)
}
}

using (ApplyStyle(output, ConsoleThemeStyle.String, ref count))
using (ApplyStyle(output, ConsoleThemeStyle.Scalar, ref count))
JsonValueFormatter.WriteQuotedJsonString(value.ToString(), output);

return count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ protected StyleReset ApplyStyle(TextWriter output, ConsoleThemeStyle style, ref
return _theme.Apply(output, style, ref invisibleCharacterCount);
}

public int Format(LogEventPropertyValue value, TextWriter output, string format)
public int Format(LogEventPropertyValue value, TextWriter output, string format, bool literalTopLevel = false)
{
return Visit(new ThemedValueFormatterState { Output = output, Format = format }, value);
return Visit(new ThemedValueFormatterState { Output = output, Format = format, IsTopLevel = literalTopLevel }, value);
}

public abstract ThemedValueFormatter SwitchTheme(ConsoleTheme theme);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct ThemedValueFormatterState
{
public TextWriter Output;
public string Format;
public bool IsTopLevel;

public ThemedValueFormatterState Nest()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,15 @@ int RenderAlignedPropertyTokenUnbuffered(PropertyToken pt, TextWriter output, Lo

int RenderValue(ConsoleTheme theme, ThemedValueFormatter valueFormatter, LogEventPropertyValue propertyValue, TextWriter output, string format)
{
if (_isLiteral && propertyValue is ScalarValue sv && (sv.Value is string str || sv.Value is char ch))
if (_isLiteral && propertyValue is ScalarValue sv && sv.Value is string)
{
var count = 0;
using (theme.Apply(output, ConsoleThemeStyle.String, ref count))
output.Write(sv.Value);
return count;
}

return valueFormatter.Format(propertyValue, output, format);
return valueFormatter.Format(propertyValue, output, format, _isLiteral);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static class AnsiConsoleThemes
[ConsoleThemeStyle.String] = "\x1b[36;1m",
[ConsoleThemeStyle.Number] = "\x1b[35;1m",
[ConsoleThemeStyle.Boolean] = "\x1b[34;1m",
[ConsoleThemeStyle.Object] = "\x1b[32;1m",
[ConsoleThemeStyle.Scalar] = "\x1b[32;1m",
[ConsoleThemeStyle.LevelVerbose] = "\x1b[37m",
[ConsoleThemeStyle.LevelDebug] = "\x1b[37m",
[ConsoleThemeStyle.LevelInformation] = "\x1b[37;1m",
Expand All @@ -51,7 +51,7 @@ static class AnsiConsoleThemes
[ConsoleThemeStyle.String] = "\x1b[1m\x1b[37;1m",
[ConsoleThemeStyle.Number] = "\x1b[1m\x1b[37;1m",
[ConsoleThemeStyle.Boolean] = "\x1b[1m\x1b[37;1m",
[ConsoleThemeStyle.Object] = "\x1b[1m\x1b[37;1m",
[ConsoleThemeStyle.Scalar] = "\x1b[1m\x1b[37;1m",
[ConsoleThemeStyle.LevelVerbose] = "\x1b[30;1m",
[ConsoleThemeStyle.LevelDebug] = "\x1b[30;1m",
[ConsoleThemeStyle.LevelInformation] ="\x1b[37;1m",
Expand All @@ -72,7 +72,7 @@ static class AnsiConsoleThemes
[ConsoleThemeStyle.String] = "\x1b[38;5;0216m",
[ConsoleThemeStyle.Number] = "\x1b[38;5;151m",
[ConsoleThemeStyle.Boolean] = "\x1b[38;5;0038m",
[ConsoleThemeStyle.Object] = "\x1b[38;5;0079m",
[ConsoleThemeStyle.Scalar] = "\x1b[38;5;0079m",
[ConsoleThemeStyle.LevelVerbose] = "\x1b[37m",
[ConsoleThemeStyle.LevelDebug] = "\x1b[37m",
[ConsoleThemeStyle.LevelInformation] = "\x1b[37;1m",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.ComponentModel;

namespace Serilog.Sinks.SystemConsole.Themes
{
/// <summary>
Expand Down Expand Up @@ -66,10 +69,16 @@ public enum ConsoleThemeStyle
/// </summary>
Boolean,

/// <summary>
/// All other scalar values, e.g. <see cref="System.Guid"/> instances.
/// </summary>
Scalar,

/// <summary>
/// Unrecogized literal values, e.g. <see cref="System.Guid"/> instances.
/// </summary>
Object,
[Obsolete("Use ConsoleThemeStyle.Scalar instead"), EditorBrowsable(EditorBrowsableState.Never)]
Object = Scalar,

/// <summary>
/// Level indicator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static class SystemConsoleThemes
[ConsoleThemeStyle.String] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.Cyan },
[ConsoleThemeStyle.Number] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.Magenta },
[ConsoleThemeStyle.Boolean] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.Blue },
[ConsoleThemeStyle.Object] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.Green },
[ConsoleThemeStyle.Scalar] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.Green },
[ConsoleThemeStyle.LevelVerbose] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.Gray },
[ConsoleThemeStyle.LevelDebug] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.Gray },
[ConsoleThemeStyle.LevelInformation] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.White },
Expand All @@ -52,7 +52,7 @@ static class SystemConsoleThemes
[ConsoleThemeStyle.String] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.White },
[ConsoleThemeStyle.Number] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.White },
[ConsoleThemeStyle.Boolean] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.White },
[ConsoleThemeStyle.Object] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.White },
[ConsoleThemeStyle.Scalar] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.White },
[ConsoleThemeStyle.LevelVerbose] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.DarkGray },
[ConsoleThemeStyle.LevelDebug] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.DarkGray },
[ConsoleThemeStyle.LevelInformation] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.White },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ public TestThemedJsonValueFormatter()
public string Format(object literal)
{
var output = new StringWriter();
Format(new ScalarValue(literal), output, null);
return output.ToString();
Format(new SequenceValue(new [] {new ScalarValue(literal)}), output, null);
var o = output.ToString();
return o.Substring(1, o.Length - 2);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
using System.Linq;
using System.Text;
using Xunit;
using Serilog.Core;
using Serilog.Sinks.SystemConsole.Themes;
using Serilog.Sinks.SystemConsole.Formatting;
using Serilog.Sinks.SystemConsole.Rendering;
using MessageTemplateParser = Serilog.Parsing.MessageTemplateParser;

namespace Serilog.Sinks.Console.Tests.Rendering
{
Expand All @@ -17,8 +15,9 @@ public class ThemedMessageTemplateRendererTests
class Chair
{
// ReSharper disable UnusedMember.Local
public string Back { get { return "straight"; } }
public int[] Legs { get { return new[] { 1, 2, 3, 4 }; } }
public string Back => "straight";

public int[] Legs => new[] { 1, 2, 3, 4 };
// ReSharper restore UnusedMember.Local
public override string ToString()
{
Expand All @@ -29,9 +28,11 @@ public override string ToString()
class Receipt
{
// ReSharper disable UnusedMember.Local
public decimal Sum { get { return 12.345m; } }
public DateTime When { get { return new DateTime(2013, 5, 20, 16, 39, 0); } }
public decimal Sum => 12.345m;

public DateTime When => new DateTime(2013, 5, 20, 16, 39, 0);
// ReSharper restore UnusedMember.Local

public override string ToString()
{
return "a receipt";
Expand Down