diff --git a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Formatting/ThemedDisplayValueFormatter.cs b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Formatting/ThemedDisplayValueFormatter.cs index 38ce0b7..a117448 100644 --- a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Formatting/ThemedDisplayValueFormatter.cs +++ b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Formatting/ThemedDisplayValueFormatter.cs @@ -39,7 +39,7 @@ protected override int VisitScalarValue(ThemedValueFormatterState state, ScalarV { if (scalar == null) throw new ArgumentNullException(nameof(scalar)); - return FormatLiteralValue(scalar, state.Output, state.Format); + return FormatLiteralValue(scalar, state.Output, state.Format, state.logEventLevel); } protected override int VisitSequenceValue(ThemedValueFormatterState state, SequenceValue sequence) @@ -49,7 +49,7 @@ protected override int VisitSequenceValue(ThemedValueFormatterState state, Seque var count = 0; - using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count, state.logEventLevel)) state.Output.Write('['); var delim = string.Empty; @@ -57,7 +57,7 @@ protected override int VisitSequenceValue(ThemedValueFormatterState state, Seque { if (delim.Length != 0) { - using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count, state.logEventLevel)) state.Output.Write(delim); } @@ -65,7 +65,7 @@ protected override int VisitSequenceValue(ThemedValueFormatterState state, Seque Visit(state, sequence.Elements[index]); } - using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count, state.logEventLevel)) state.Output.Write(']'); return count; @@ -77,13 +77,13 @@ protected override int VisitStructureValue(ThemedValueFormatterState state, Stru if (structure.TypeTag != null) { - using (ApplyStyle(state.Output, ConsoleThemeStyle.Name, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.Name, ref count, state.logEventLevel)) state.Output.Write(structure.TypeTag); state.Output.Write(' '); } - using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count, state.logEventLevel)) state.Output.Write('{'); var delim = string.Empty; @@ -91,7 +91,7 @@ protected override int VisitStructureValue(ThemedValueFormatterState state, Stru { if (delim.Length != 0) { - using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count, state.logEventLevel)) state.Output.Write(delim); } @@ -99,16 +99,16 @@ protected override int VisitStructureValue(ThemedValueFormatterState state, Stru var property = structure.Properties[index]; - using (ApplyStyle(state.Output, ConsoleThemeStyle.Name, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.Name, ref count, state.logEventLevel)) state.Output.Write(property.Name); - using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count, state.logEventLevel)) state.Output.Write('='); count += Visit(state.Nest(), property.Value); } - using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count, state.logEventLevel)) state.Output.Write('}'); return count; @@ -118,7 +118,7 @@ protected override int VisitDictionaryValue(ThemedValueFormatterState state, Dic { var count = 0; - using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count, state.logEventLevel)) state.Output.Write('{'); var delim = string.Empty; @@ -126,45 +126,45 @@ protected override int VisitDictionaryValue(ThemedValueFormatterState state, Dic { if (delim.Length != 0) { - using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count, state.logEventLevel)) state.Output.Write(delim); } delim = ", "; - using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count, state.logEventLevel)) state.Output.Write('['); - using (ApplyStyle(state.Output, ConsoleThemeStyle.String, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.String, ref count, state.logEventLevel)) count += Visit(state.Nest(), element.Key); - using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count, state.logEventLevel)) state.Output.Write("]="); count += Visit(state.Nest(), element.Value); } - using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count, state.logEventLevel)) state.Output.Write('}'); return count; } - public int FormatLiteralValue(ScalarValue scalar, TextWriter output, string format) + public int FormatLiteralValue(ScalarValue scalar, TextWriter output, string format, LogEventLevel logEventLevel) { var value = scalar.Value; var count = 0; if (value == null) { - using (ApplyStyle(output, ConsoleThemeStyle.Null, ref count)) + using (ApplyStyle(output, ConsoleThemeStyle.Null, ref count, logEventLevel)) output.Write("null"); return count; } if (value is string str) { - using (ApplyStyle(output, ConsoleThemeStyle.String, ref count)) + using (ApplyStyle(output, ConsoleThemeStyle.String, ref count, logEventLevel)) { if (format != "l") JsonValueFormatter.WriteQuotedJsonString(str, output); @@ -180,14 +180,14 @@ public int FormatLiteralValue(ScalarValue scalar, TextWriter output, string form value is decimal || value is byte || value is sbyte || value is short || value is ushort || value is float || value is double) { - using (ApplyStyle(output, ConsoleThemeStyle.Number, ref count)) + using (ApplyStyle(output, ConsoleThemeStyle.Number, ref count, logEventLevel)) scalar.Render(output, format, _formatProvider); return count; } if (value is bool b) { - using (ApplyStyle(output, ConsoleThemeStyle.Boolean, ref count)) + using (ApplyStyle(output, ConsoleThemeStyle.Boolean, ref count, logEventLevel)) output.Write(b); return count; @@ -195,7 +195,7 @@ value is decimal || value is byte || value is sbyte || value is short || if (value is char ch) { - using (ApplyStyle(output, ConsoleThemeStyle.Scalar, ref count)) + using (ApplyStyle(output, ConsoleThemeStyle.Scalar, ref count, logEventLevel)) { output.Write('\''); output.Write(ch); @@ -205,7 +205,7 @@ value is decimal || value is byte || value is sbyte || value is short || } } - using (ApplyStyle(output, ConsoleThemeStyle.Scalar, ref count)) + using (ApplyStyle(output, ConsoleThemeStyle.Scalar, ref count, logEventLevel)) scalar.Render(output, format, _formatProvider); return count; diff --git a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Formatting/ThemedJsonValueFormatter.cs b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Formatting/ThemedJsonValueFormatter.cs index 2294ea7..af9d455 100644 --- a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Formatting/ThemedJsonValueFormatter.cs +++ b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Formatting/ThemedJsonValueFormatter.cs @@ -45,9 +45,9 @@ protected override int VisitScalarValue(ThemedValueFormatterState state, ScalarV // At the top level, for scalar values, use "display" rendering. if (state.IsTopLevel) - return _displayFormatter.FormatLiteralValue(scalar, state.Output, state.Format); + return _displayFormatter.FormatLiteralValue(scalar, state.Output, state.Format, state.logEventLevel); - return FormatLiteralValue(scalar, state.Output); + return FormatLiteralValue(scalar, state.Output, state.logEventLevel); } protected override int VisitSequenceValue(ThemedValueFormatterState state, SequenceValue sequence) @@ -57,7 +57,7 @@ protected override int VisitSequenceValue(ThemedValueFormatterState state, Seque var count = 0; - using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count, state.logEventLevel)) state.Output.Write('['); var delim = string.Empty; @@ -65,7 +65,7 @@ protected override int VisitSequenceValue(ThemedValueFormatterState state, Seque { if (delim.Length != 0) { - using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count, state.logEventLevel)) state.Output.Write(delim); } @@ -73,7 +73,7 @@ protected override int VisitSequenceValue(ThemedValueFormatterState state, Seque Visit(state.Nest(), sequence.Elements[index]); } - using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count, state.logEventLevel)) state.Output.Write(']'); return count; @@ -83,7 +83,7 @@ protected override int VisitStructureValue(ThemedValueFormatterState state, Stru { var count = 0; - using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count, state.logEventLevel)) state.Output.Write('{'); var delim = string.Empty; @@ -91,7 +91,7 @@ protected override int VisitStructureValue(ThemedValueFormatterState state, Stru { if (delim.Length != 0) { - using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count, state.logEventLevel)) state.Output.Write(delim); } @@ -99,10 +99,10 @@ protected override int VisitStructureValue(ThemedValueFormatterState state, Stru var property = structure.Properties[index]; - using (ApplyStyle(state.Output, ConsoleThemeStyle.Name, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.Name, ref count, state.logEventLevel)) JsonValueFormatter.WriteQuotedJsonString(property.Name, state.Output); - using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count, state.logEventLevel)) state.Output.Write(": "); count += Visit(state.Nest(), property.Value); @@ -110,20 +110,20 @@ protected override int VisitStructureValue(ThemedValueFormatterState state, Stru if (structure.TypeTag != null) { - using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count, state.logEventLevel)) state.Output.Write(delim); - using (ApplyStyle(state.Output, ConsoleThemeStyle.Name, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.Name, ref count, state.logEventLevel)) JsonValueFormatter.WriteQuotedJsonString("$type", state.Output); - using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count, state.logEventLevel)) state.Output.Write(": "); - using (ApplyStyle(state.Output, ConsoleThemeStyle.String, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.String, ref count, state.logEventLevel)) JsonValueFormatter.WriteQuotedJsonString(structure.TypeTag, state.Output); } - using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count, state.logEventLevel)) state.Output.Write('}'); return count; @@ -133,7 +133,7 @@ protected override int VisitDictionaryValue(ThemedValueFormatterState state, Dic { var count = 0; - using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count, state.logEventLevel)) state.Output.Write('{'); var delim = string.Empty; @@ -141,7 +141,7 @@ protected override int VisitDictionaryValue(ThemedValueFormatterState state, Dic { if (delim.Length != 0) { - using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count, state.logEventLevel)) state.Output.Write(delim); } @@ -153,36 +153,36 @@ protected override int VisitDictionaryValue(ThemedValueFormatterState state, Dic ? ConsoleThemeStyle.String : ConsoleThemeStyle.Scalar; - using (ApplyStyle(state.Output, style, ref count)) + using (ApplyStyle(state.Output, style, ref count, state.logEventLevel)) JsonValueFormatter.WriteQuotedJsonString((element.Key.Value ?? "null").ToString(), state.Output); - using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count, state.logEventLevel)) state.Output.Write(": "); count += Visit(state.Nest(), element.Value); } - using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count)) + using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count, state.logEventLevel)) state.Output.Write('}'); return count; } - int FormatLiteralValue(ScalarValue scalar, TextWriter output) + int FormatLiteralValue(ScalarValue scalar, TextWriter output, LogEventLevel logEventLevel) { var value = scalar.Value; var count = 0; if (value == null) { - using (ApplyStyle(output, ConsoleThemeStyle.Null, ref count)) + using (ApplyStyle(output, ConsoleThemeStyle.Null, ref count, logEventLevel)) output.Write("null"); return count; } if (value is string str) { - using (ApplyStyle(output, ConsoleThemeStyle.String, ref count)) + using (ApplyStyle(output, ConsoleThemeStyle.String, ref count, logEventLevel)) JsonValueFormatter.WriteQuotedJsonString(str, output); return count; } @@ -191,14 +191,14 @@ int FormatLiteralValue(ScalarValue scalar, TextWriter output) { 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)) + using (ApplyStyle(output, ConsoleThemeStyle.Number, ref count, logEventLevel)) output.Write(((IFormattable)value).ToString(null, CultureInfo.InvariantCulture)); return count; } if (value is double d) { - using (ApplyStyle(output, ConsoleThemeStyle.Number, ref count)) + using (ApplyStyle(output, ConsoleThemeStyle.Number, ref count, logEventLevel)) { if (double.IsNaN(d) || double.IsInfinity(d)) JsonValueFormatter.WriteQuotedJsonString(d.ToString(CultureInfo.InvariantCulture), output); @@ -210,7 +210,7 @@ int FormatLiteralValue(ScalarValue scalar, TextWriter output) if (value is float f) { - using (ApplyStyle(output, ConsoleThemeStyle.Number, ref count)) + using (ApplyStyle(output, ConsoleThemeStyle.Number, ref count, logEventLevel)) { if (double.IsNaN(f) || double.IsInfinity(f)) JsonValueFormatter.WriteQuotedJsonString(f.ToString(CultureInfo.InvariantCulture), output); @@ -222,7 +222,7 @@ int FormatLiteralValue(ScalarValue scalar, TextWriter output) if (value is bool b) { - using (ApplyStyle(output, ConsoleThemeStyle.Boolean, ref count)) + using (ApplyStyle(output, ConsoleThemeStyle.Boolean, ref count, logEventLevel)) output.Write(b ? "true" : "false"); return count; @@ -230,14 +230,14 @@ int FormatLiteralValue(ScalarValue scalar, TextWriter output) if (value is char ch) { - using (ApplyStyle(output, ConsoleThemeStyle.Scalar, ref count)) + using (ApplyStyle(output, ConsoleThemeStyle.Scalar, ref count, logEventLevel)) JsonValueFormatter.WriteQuotedJsonString(ch.ToString(), output); return count; } if (value is DateTime || value is DateTimeOffset) { - using (ApplyStyle(output, ConsoleThemeStyle.Scalar, ref count)) + using (ApplyStyle(output, ConsoleThemeStyle.Scalar, ref count, logEventLevel)) { output.Write('"'); output.Write(((IFormattable)value).ToString("O", CultureInfo.InvariantCulture)); @@ -247,7 +247,7 @@ int FormatLiteralValue(ScalarValue scalar, TextWriter output) } } - using (ApplyStyle(output, ConsoleThemeStyle.Scalar, ref count)) + using (ApplyStyle(output, ConsoleThemeStyle.Scalar, ref count, logEventLevel)) JsonValueFormatter.WriteQuotedJsonString(value.ToString(), output); return count; diff --git a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Formatting/ThemedValueFormatter.cs b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Formatting/ThemedValueFormatter.cs index ea780e0..7130347 100644 --- a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Formatting/ThemedValueFormatter.cs +++ b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Formatting/ThemedValueFormatter.cs @@ -29,12 +29,12 @@ protected ThemedValueFormatter(ConsoleTheme theme) _theme = theme ?? throw new ArgumentNullException(nameof(theme)); } - protected StyleReset ApplyStyle(TextWriter output, ConsoleThemeStyle style, ref int invisibleCharacterCount) + protected StyleReset ApplyStyle(TextWriter output, ConsoleThemeStyle style, ref int invisibleCharacterCount, LogEventLevel logEventLevel) { - return _theme.Apply(output, style, ref invisibleCharacterCount); + return _theme.Apply(output, style, ref invisibleCharacterCount, logEventLevel); } - public int Format(LogEventPropertyValue value, TextWriter output, string format, bool literalTopLevel = false) + public int Format(LogEventPropertyValue value, TextWriter output, string format, LogEventLevel logEventLevel, bool literalTopLevel = false) { return Visit(new ThemedValueFormatterState { Output = output, Format = format, IsTopLevel = literalTopLevel }, value); } diff --git a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Formatting/ThemedValueFormatterState.cs b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Formatting/ThemedValueFormatterState.cs index 5fa497e..bf96aad 100644 --- a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Formatting/ThemedValueFormatterState.cs +++ b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Formatting/ThemedValueFormatterState.cs @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +using Serilog.Events; using System.IO; namespace Serilog.Sinks.SystemConsole.Formatting @@ -21,7 +22,7 @@ struct ThemedValueFormatterState public TextWriter Output; public string Format; public bool IsTopLevel; - - public ThemedValueFormatterState Nest() => new ThemedValueFormatterState { Output = Output }; + public LogEventLevel logEventLevel; + public ThemedValueFormatterState Nest() => new ThemedValueFormatterState { Output = Output, logEventLevel = logEventLevel }; } } diff --git a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/EventPropertyTokenRenderer.cs b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/EventPropertyTokenRenderer.cs index 19233ea..793d2a1 100644 --- a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/EventPropertyTokenRenderer.cs +++ b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/EventPropertyTokenRenderer.cs @@ -44,7 +44,7 @@ public override void Render(LogEvent logEvent, TextWriter output) } var _ = 0; - using (_theme.Apply(output, ConsoleThemeStyle.SecondaryText, ref _)) + using (_theme.Apply(output, ConsoleThemeStyle.SecondaryText, ref _, logEvent.Level)) { var writer = _token.Alignment.HasValue ? new StringWriter() : output; diff --git a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/ExceptionTokenRenderer.cs b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/ExceptionTokenRenderer.cs index 31aa4c2..70481ee 100644 --- a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/ExceptionTokenRenderer.cs +++ b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/ExceptionTokenRenderer.cs @@ -43,7 +43,7 @@ public override void Render(LogEvent logEvent, TextWriter output) { var style = nextLine.StartsWith(StackFrameLinePrefix) ? ConsoleThemeStyle.SecondaryText : ConsoleThemeStyle.Text; var _ = 0; - using (_theme.Apply(output, style, ref _)) + using (_theme.Apply(output, style, ref _, logEvent.Level)) output.WriteLine(nextLine); } } diff --git a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/LevelTokenRenderer.cs b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/LevelTokenRenderer.cs index 54f90a2..a21a8e3 100644 --- a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/LevelTokenRenderer.cs +++ b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/LevelTokenRenderer.cs @@ -53,7 +53,7 @@ public override void Render(LogEvent logEvent, TextWriter output) levelStyle = ConsoleThemeStyle.Invalid; var _ = 0; - using (_theme.Apply(output, levelStyle, ref _)) + using (_theme.Apply(output, levelStyle, ref _, logEvent.Level)) Padding.Apply(output, moniker, _levelToken.Alignment); } } diff --git a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/MessageTemplateOutputTokenRenderer.cs b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/MessageTemplateOutputTokenRenderer.cs index 6ca5fb4..0f8840c 100644 --- a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/MessageTemplateOutputTokenRenderer.cs +++ b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/MessageTemplateOutputTokenRenderer.cs @@ -56,12 +56,12 @@ public override void Render(LogEvent logEvent, TextWriter output) { if (_token.Alignment == null || !_theme.CanBuffer) { - _renderer.Render(logEvent.MessageTemplate, logEvent.Properties, output); + _renderer.Render(logEvent.MessageTemplate, logEvent.Properties, output, logEvent.Level); return; } var buffer = new StringWriter(); - var invisible = _renderer.Render(logEvent.MessageTemplate, logEvent.Properties, buffer); + var invisible = _renderer.Render(logEvent.MessageTemplate, logEvent.Properties, buffer, logEvent.Level); var value = buffer.ToString(); Padding.Apply(output, value, _token.Alignment.Value.Widen(invisible)); } diff --git a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/PropertiesTokenRenderer.cs b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/PropertiesTokenRenderer.cs index 8bfe16e..855020b 100644 --- a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/PropertiesTokenRenderer.cs +++ b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/PropertiesTokenRenderer.cs @@ -63,12 +63,12 @@ public override void Render(LogEvent logEvent, TextWriter output) if (_token.Alignment == null || !_theme.CanBuffer) { - _valueFormatter.Format(value, output, null); + _valueFormatter.Format(value, output, null, logEvent.Level); return; } var buffer = new StringWriter(new StringBuilder(value.Properties.Count * 16)); - var invisible = _valueFormatter.Format(value, buffer, null); + var invisible = _valueFormatter.Format(value, buffer, null, logEvent.Level); var str = buffer.ToString(); Padding.Apply(output, str, _token.Alignment.Value.Widen(invisible)); } diff --git a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/TextTokenRenderer.cs b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/TextTokenRenderer.cs index 1394964..4eaab86 100644 --- a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/TextTokenRenderer.cs +++ b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/TextTokenRenderer.cs @@ -32,7 +32,7 @@ public TextTokenRenderer(ConsoleTheme theme, string text) public override void Render(LogEvent logEvent, TextWriter output) { var _ = 0; - using (_theme.Apply(output, ConsoleThemeStyle.TertiaryText, ref _)) + using (_theme.Apply(output, ConsoleThemeStyle.TertiaryText, ref _, logEvent.Level)) output.Write(_text); } } diff --git a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/TimestampTokenRenderer.cs b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/TimestampTokenRenderer.cs index 5eac5e2..7126dc2 100644 --- a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/TimestampTokenRenderer.cs +++ b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/TimestampTokenRenderer.cs @@ -41,7 +41,7 @@ public override void Render(LogEvent logEvent, TextWriter output) var sv = new ScalarValue(logEvent.Timestamp); var _ = 0; - using (_theme.Apply(output, ConsoleThemeStyle.SecondaryText, ref _)) + using (_theme.Apply(output, ConsoleThemeStyle.SecondaryText, ref _, logEvent.Level)) { if (_token.Alignment == null) { diff --git a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Rendering/ThemedMessageTemplateRenderer.cs b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Rendering/ThemedMessageTemplateRenderer.cs index 3d16ee0..8c5701d 100644 --- a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Rendering/ThemedMessageTemplateRenderer.cs +++ b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Rendering/ThemedMessageTemplateRenderer.cs @@ -38,53 +38,53 @@ public ThemedMessageTemplateRenderer(ConsoleTheme theme, ThemedValueFormatter va _unthemedValueFormatter = valueFormatter.SwitchTheme(NoTheme); } - public int Render(MessageTemplate template, IReadOnlyDictionary properties, TextWriter output) + public int Render(MessageTemplate template, IReadOnlyDictionary properties, TextWriter output, LogEventLevel logEventLevel) { var count = 0; foreach (var token in template.Tokens) { if (token is TextToken tt) { - count += RenderTextToken(tt, output); + count += RenderTextToken(tt, output, logEventLevel); } else { var pt = (PropertyToken)token; - count += RenderPropertyToken(pt, properties, output); + count += RenderPropertyToken(pt, properties, output, logEventLevel); } } return count; } - int RenderTextToken(TextToken tt, TextWriter output) + int RenderTextToken(TextToken tt, TextWriter output, LogEventLevel logEventLevel) { var count = 0; - using (_theme.Apply(output, ConsoleThemeStyle.Text, ref count)) + using (_theme.Apply(output, ConsoleThemeStyle.Text, ref count, logEventLevel)) output.Write(tt.Text); return count; } - int RenderPropertyToken(PropertyToken pt, IReadOnlyDictionary properties, TextWriter output) + int RenderPropertyToken(PropertyToken pt, IReadOnlyDictionary properties, TextWriter output, LogEventLevel logEventLevel) { if (!properties.TryGetValue(pt.PropertyName, out var propertyValue)) { var count = 0; - using (_theme.Apply(output, ConsoleThemeStyle.Invalid, ref count)) + using (_theme.Apply(output, ConsoleThemeStyle.Invalid, ref count, logEventLevel)) output.Write(pt.ToString()); return count; } if (!pt.Alignment.HasValue) { - return RenderValue(_theme, _valueFormatter, propertyValue, output, pt.Format); + return RenderValue(_theme, _valueFormatter, propertyValue, output, pt.Format, logEventLevel); } var valueOutput = new StringWriter(); if (!_theme.CanBuffer) - return RenderAlignedPropertyTokenUnbuffered(pt, output, propertyValue); + return RenderAlignedPropertyTokenUnbuffered(pt, output, propertyValue, logEventLevel); - var invisibleCount = RenderValue(_theme, _valueFormatter, propertyValue, valueOutput, pt.Format); + var invisibleCount = RenderValue(_theme, _valueFormatter, propertyValue, valueOutput, pt.Format, logEventLevel); var value = valueOutput.ToString(); @@ -100,40 +100,40 @@ int RenderPropertyToken(PropertyToken pt, IReadOnlyDictionary= pt.Alignment.Value.Width) { - return RenderValue(_theme, _valueFormatter, propertyValue, output, pt.Format); + return RenderValue(_theme, _valueFormatter, propertyValue, output, pt.Format, logEventLevel); } if (pt.Alignment.Value.Direction == AlignmentDirection.Left) { - var invisible = RenderValue(_theme, _valueFormatter, propertyValue, output, pt.Format); + var invisible = RenderValue(_theme, _valueFormatter, propertyValue, output, pt.Format, logEventLevel); Padding.Apply(output, string.Empty, pt.Alignment.Value.Widen(-valueLength)); return invisible; } Padding.Apply(output, string.Empty, pt.Alignment.Value.Widen(-valueLength)); - return RenderValue(_theme, _valueFormatter, propertyValue, output, pt.Format); + return RenderValue(_theme, _valueFormatter, propertyValue, output, pt.Format, logEventLevel); } - int RenderValue(ConsoleTheme theme, ThemedValueFormatter valueFormatter, LogEventPropertyValue propertyValue, TextWriter output, string format) + int RenderValue(ConsoleTheme theme, ThemedValueFormatter valueFormatter, LogEventPropertyValue propertyValue, TextWriter output, string format, LogEventLevel logEventLevel) { if (_isLiteral && propertyValue is ScalarValue sv && sv.Value is string) { var count = 0; - using (theme.Apply(output, ConsoleThemeStyle.String, ref count)) + using (theme.Apply(output, ConsoleThemeStyle.String, ref count, logEventLevel)) output.Write(sv.Value); return count; } - return valueFormatter.Format(propertyValue, output, format, _isLiteral); + return valueFormatter.Format(propertyValue, output, format, logEventLevel, _isLiteral); } } } diff --git a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/AnsiConsoleTheme.cs b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/AnsiConsoleTheme.cs index 3ff6ce8..b343149 100644 --- a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/AnsiConsoleTheme.cs +++ b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/AnsiConsoleTheme.cs @@ -60,8 +60,14 @@ public AnsiConsoleTheme(IReadOnlyDictionary styles) protected override int ResetCharCount { get; } = AnsiStyleReset.Length; /// - public override int Set(TextWriter output, ConsoleThemeStyle style) + public override int Set(TextWriter output, ConsoleThemeStyle style, ConsoleThemeStyle levelLineStyle) { + if (_styles.TryGetValue(levelLineStyle, out var ansiLineStyle)) + { + output.Write(ansiLineStyle); + return ansiLineStyle.Length; + } + if (_styles.TryGetValue(style, out var ansiStyle)) { output.Write(ansiStyle); diff --git a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/ConsoleTheme.cs b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/ConsoleTheme.cs index b3921bf..d39feec 100644 --- a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/ConsoleTheme.cs +++ b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/ConsoleTheme.cs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +using Serilog.Events; +using System.Collections.Generic; using System.IO; namespace Serilog.Sinks.SystemConsole.Themes @@ -21,6 +23,16 @@ namespace Serilog.Sinks.SystemConsole.Themes /// public abstract class ConsoleTheme { + static readonly Dictionary Levels = new Dictionary + { + { LogEventLevel.Verbose, ConsoleThemeStyle.LevelVerboseLine }, + { LogEventLevel.Debug, ConsoleThemeStyle.LevelDebugLine }, + { LogEventLevel.Information, ConsoleThemeStyle.LevelInformationLine }, + { LogEventLevel.Warning, ConsoleThemeStyle.LevelWarningLine }, + { LogEventLevel.Error, ConsoleThemeStyle.LevelErrorLine }, + { LogEventLevel.Fatal, ConsoleThemeStyle.LevelFatalLine }, + }; + /// /// No styling applied. /// @@ -37,8 +49,9 @@ public abstract class ConsoleTheme /// /// Output destination. /// Style to apply. + /// Style to apply to the entire line. /// The number of characters written to . - public abstract int Set(TextWriter output, ConsoleThemeStyle style); + public abstract int Set(TextWriter output, ConsoleThemeStyle style, ConsoleThemeStyle levelLineStyle); /// /// Reset the output to un-styled colors. @@ -51,9 +64,12 @@ public abstract class ConsoleTheme /// protected abstract int ResetCharCount { get; } - internal StyleReset Apply(TextWriter output, ConsoleThemeStyle style, ref int invisibleCharacterCount) + internal StyleReset Apply(TextWriter output, ConsoleThemeStyle style, ref int invisibleCharacterCount, LogEventLevel logEventLevel) { - invisibleCharacterCount += Set(output, style); + if (!Levels.TryGetValue(logEventLevel, out var levelLineStyle)) + levelLineStyle = ConsoleThemeStyle.Invalid; + + invisibleCharacterCount += Set(output, style, levelLineStyle); invisibleCharacterCount += ResetCharCount; return new StyleReset(this, output); diff --git a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/ConsoleThemeStyle.cs b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/ConsoleThemeStyle.cs index 69d231f..95c688e 100644 --- a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/ConsoleThemeStyle.cs +++ b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/ConsoleThemeStyle.cs @@ -110,5 +110,35 @@ public enum ConsoleThemeStyle /// Level indicator. /// LevelFatal, + + /// + /// Level indicator. + /// + LevelVerboseLine, + + /// + /// Level indicator. + /// + LevelDebugLine, + + /// + /// Level indicator. + /// + LevelInformationLine, + + /// + /// Level indicator. + /// + LevelWarningLine, + + /// + /// Level indicator. + /// + LevelErrorLine, + + /// + /// Level indicator. + /// + LevelFatalLine, } } \ No newline at end of file diff --git a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/EmptyConsoleTheme.cs b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/EmptyConsoleTheme.cs index de74f8c..415f630 100644 --- a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/EmptyConsoleTheme.cs +++ b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/EmptyConsoleTheme.cs @@ -22,7 +22,7 @@ class EmptyConsoleTheme : ConsoleTheme protected override int ResetCharCount { get; } - public override int Set(TextWriter output, ConsoleThemeStyle style) => 0; + public override int Set(TextWriter output, ConsoleThemeStyle style, ConsoleThemeStyle levelLineStyle) => 0; public override void Reset(TextWriter output) { diff --git a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/SystemConsoleTheme.cs b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/SystemConsoleTheme.cs index 5eaf7d8..60dde12 100644 --- a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/SystemConsoleTheme.cs +++ b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/SystemConsoleTheme.cs @@ -60,8 +60,16 @@ public SystemConsoleTheme(IReadOnlyDictionary - public override int Set(TextWriter output, ConsoleThemeStyle style) + public override int Set(TextWriter output, ConsoleThemeStyle style, ConsoleThemeStyle levelLineStyle) { + if (Styles.TryGetValue(levelLineStyle, out var wctls)) + { + if (wctls.Foreground.HasValue) + Console.ForegroundColor = wctls.Foreground.Value; + if (wctls.Background.HasValue) + Console.BackgroundColor = wctls.Background.Value; + } + if (Styles.TryGetValue(style, out var wcts)) { if (wcts.Foreground.HasValue) diff --git a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/SystemConsoleThemes.cs b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/SystemConsoleThemes.cs index 5e202f9..4dbfaca 100644 --- a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/SystemConsoleThemes.cs +++ b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/SystemConsoleThemes.cs @@ -38,6 +38,13 @@ static class SystemConsoleThemes [ConsoleThemeStyle.LevelWarning] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.Yellow }, [ConsoleThemeStyle.LevelError] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.White, Background = ConsoleColor.Red }, [ConsoleThemeStyle.LevelFatal] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.White, Background = ConsoleColor.Red }, + [ConsoleThemeStyle.LevelVerboseLine] = new SystemConsoleThemeStyle { Background = ConsoleColor.DarkBlue }, + [ConsoleThemeStyle.LevelDebugLine] = new SystemConsoleThemeStyle { Background = ConsoleColor.DarkGray }, + [ConsoleThemeStyle.LevelInformationLine] = new SystemConsoleThemeStyle { }, + [ConsoleThemeStyle.LevelWarningLine] = new SystemConsoleThemeStyle { Background = ConsoleColor.DarkYellow }, + [ConsoleThemeStyle.LevelErrorLine] = new SystemConsoleThemeStyle { Background = ConsoleColor.DarkRed }, + [ConsoleThemeStyle.LevelFatalLine] = new SystemConsoleThemeStyle { Background = ConsoleColor.Red }, + }); public static SystemConsoleTheme Grayscale { get; } = new SystemConsoleTheme( diff --git a/test/Serilog.Sinks.Console.Tests/Formatting/ThemedDisplayValueFormatterTests.cs b/test/Serilog.Sinks.Console.Tests/Formatting/ThemedDisplayValueFormatterTests.cs index 2770069..c2d77ff 100644 --- a/test/Serilog.Sinks.Console.Tests/Formatting/ThemedDisplayValueFormatterTests.cs +++ b/test/Serilog.Sinks.Console.Tests/Formatting/ThemedDisplayValueFormatterTests.cs @@ -15,7 +15,7 @@ public void StringFormattingIsApplied(string value, string format, string expect { var formatter = new ThemedDisplayValueFormatter(ConsoleTheme.None, null); var sw = new StringWriter(); - formatter.FormatLiteralValue(new ScalarValue(value), sw, format); + formatter.FormatLiteralValue(new ScalarValue(value), sw, format, LogEventLevel.Information); var actual = sw.ToString(); Assert.Equal(expected, actual); } diff --git a/test/Serilog.Sinks.Console.Tests/Formatting/ThemedJsonValueFormatterTests.cs b/test/Serilog.Sinks.Console.Tests/Formatting/ThemedJsonValueFormatterTests.cs index 45a3814..92ce983 100644 --- a/test/Serilog.Sinks.Console.Tests/Formatting/ThemedJsonValueFormatterTests.cs +++ b/test/Serilog.Sinks.Console.Tests/Formatting/ThemedJsonValueFormatterTests.cs @@ -20,7 +20,7 @@ public TestThemedJsonValueFormatter() public string Format(object literal) { var output = new StringWriter(); - Format(new SequenceValue(new[] { new ScalarValue(literal) }), output, null); + Format(new SequenceValue(new[] { new ScalarValue(literal) }), output, null, LogEventLevel.Information); var o = output.ToString(); return o.Substring(1, o.Length - 2); } @@ -85,7 +85,7 @@ static string Format(LogEventPropertyValue value) { var formatter = new TestThemedJsonValueFormatter(); var output = new StringWriter(); - formatter.Format(value, output, null); + formatter.Format(value, output, null, LogEventLevel.Information); return output.ToString(); } diff --git a/test/Serilog.Sinks.Console.Tests/Rendering/ThemedMessageTemplateRendererTests.cs b/test/Serilog.Sinks.Console.Tests/Rendering/ThemedMessageTemplateRendererTests.cs index 2144cea..d41f61e 100644 --- a/test/Serilog.Sinks.Console.Tests/Rendering/ThemedMessageTemplateRendererTests.cs +++ b/test/Serilog.Sinks.Console.Tests/Rendering/ThemedMessageTemplateRendererTests.cs @@ -129,7 +129,7 @@ static string Render(IFormatProvider formatProvider, string messageTemplate, par var writer = new StringWriter(output); var renderer = new ThemedMessageTemplateRenderer(ConsoleTheme.None, new ThemedDisplayValueFormatter(ConsoleTheme.None, formatProvider), false); - renderer.Render(mt, props.ToDictionary(p => p.Name, p => p.Value), writer); + renderer.Render(mt, props.ToDictionary(p => p.Name, p => p.Value), writer, Events.LogEventLevel.Information); writer.Flush(); return output.ToString(); } diff --git a/test/Serilog.Sinks.Console.Tests/Support/TracingConsoleTheme.cs b/test/Serilog.Sinks.Console.Tests/Support/TracingConsoleTheme.cs index 901315e..d6439f0 100644 --- a/test/Serilog.Sinks.Console.Tests/Support/TracingConsoleTheme.cs +++ b/test/Serilog.Sinks.Console.Tests/Support/TracingConsoleTheme.cs @@ -11,9 +11,9 @@ class TracingConsoleTheme : ConsoleTheme protected override int ResetCharCount { get; } = End.Length; - public override int Set(TextWriter output, ConsoleThemeStyle style) + public override int Set(TextWriter output, ConsoleThemeStyle style, ConsoleThemeStyle levelLineStyle) { - var start = $"<{style.ToString().ToLowerInvariant()}>"; + var start = $"<{levelLineStyle.ToString().ToLowerInvariant()} {style.ToString().ToLowerInvariant()}>"; output.Write(start); return start.Length; }