From cbccb7511bbfad0932fe77199743c8248345b046 Mon Sep 17 00:00:00 2001 From: Rafael Cordeiro Date: Fri, 17 Jul 2020 21:51:20 -0700 Subject: [PATCH 1/2] Use faster null check --- .../ConsoleLoggerConfigurationExtensions.cs | 8 ++++---- .../Sinks/SystemConsole/ConsoleSink.cs | 2 +- .../Formatting/ThemedDisplayValueFormatter.cs | 6 +++--- .../SystemConsole/Formatting/ThemedJsonValueFormatter.cs | 2 +- .../Sinks/SystemConsole/Output/ExceptionTokenRenderer.cs | 2 +- .../Sinks/SystemConsole/Output/LevelOutputFormat.cs | 2 +- .../Output/MessageTemplateOutputTokenRenderer.cs | 2 +- .../Sinks/SystemConsole/Output/OutputTemplateRenderer.cs | 6 +++--- .../Sinks/SystemConsole/Output/PropertiesTokenRenderer.cs | 2 +- .../Sinks/SystemConsole/Output/TimestampTokenRenderer.cs | 2 +- .../Sinks/SystemConsole/Rendering/Padding.cs | 2 +- .../Sinks/SystemConsole/Themes/AnsiConsoleTheme.cs | 2 +- .../Sinks/SystemConsole/Themes/SystemConsoleTheme.cs | 2 +- 13 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/Serilog.Sinks.Console/ConsoleLoggerConfigurationExtensions.cs b/src/Serilog.Sinks.Console/ConsoleLoggerConfigurationExtensions.cs index fa6a352..2786dbd 100644 --- a/src/Serilog.Sinks.Console/ConsoleLoggerConfigurationExtensions.cs +++ b/src/Serilog.Sinks.Console/ConsoleLoggerConfigurationExtensions.cs @@ -61,8 +61,8 @@ public static LoggerConfiguration Console( bool applyThemeToRedirectedOutput = false, object syncRoot = null) { - if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration)); - if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate)); + if (sinkConfiguration is null) throw new ArgumentNullException(nameof(sinkConfiguration)); + if (outputTemplate is null) throw new ArgumentNullException(nameof(outputTemplate)); var appliedTheme = !applyThemeToRedirectedOutput && (System.Console.IsOutputRedirected || System.Console.IsErrorRedirected) ? ConsoleTheme.None : @@ -97,8 +97,8 @@ public static LoggerConfiguration Console( LogEventLevel? standardErrorFromLevel = null, object syncRoot = null) { - if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration)); - if (formatter == null) throw new ArgumentNullException(nameof(formatter)); + if (sinkConfiguration is null) throw new ArgumentNullException(nameof(sinkConfiguration)); + if (formatter is null) throw new ArgumentNullException(nameof(formatter)); syncRoot = syncRoot ?? DefaultSyncRoot; return sinkConfiguration.Sink(new ConsoleSink(ConsoleTheme.None, formatter, standardErrorFromLevel, syncRoot), restrictedToMinimumLevel, levelSwitch); diff --git a/src/Serilog.Sinks.Console/Sinks/SystemConsole/ConsoleSink.cs b/src/Serilog.Sinks.Console/Sinks/SystemConsole/ConsoleSink.cs index bbe09e0..e1c1bea 100644 --- a/src/Serilog.Sinks.Console/Sinks/SystemConsole/ConsoleSink.cs +++ b/src/Serilog.Sinks.Console/Sinks/SystemConsole/ConsoleSink.cs @@ -79,7 +79,7 @@ public void Emit(LogEvent logEvent) TextWriter SelectOutputStream(LogEventLevel logEventLevel) { - if (_standardErrorFromLevel == null) + if (_standardErrorFromLevel is null) return Console.Out; return logEventLevel < _standardErrorFromLevel ? Console.Out : Console.Error; diff --git a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Formatting/ThemedDisplayValueFormatter.cs b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Formatting/ThemedDisplayValueFormatter.cs index 38ce0b7..b072707 100644 --- a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Formatting/ThemedDisplayValueFormatter.cs +++ b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Formatting/ThemedDisplayValueFormatter.cs @@ -37,14 +37,14 @@ public override ThemedValueFormatter SwitchTheme(ConsoleTheme theme) protected override int VisitScalarValue(ThemedValueFormatterState state, ScalarValue scalar) { - if (scalar == null) + if (scalar is null) throw new ArgumentNullException(nameof(scalar)); return FormatLiteralValue(scalar, state.Output, state.Format); } protected override int VisitSequenceValue(ThemedValueFormatterState state, SequenceValue sequence) { - if (sequence == null) + if (sequence is null) throw new ArgumentNullException(nameof(sequence)); var count = 0; @@ -155,7 +155,7 @@ public int FormatLiteralValue(ScalarValue scalar, TextWriter output, string form var value = scalar.Value; var count = 0; - if (value == null) + if (value is null) { using (ApplyStyle(output, ConsoleThemeStyle.Null, ref count)) output.Write("null"); diff --git a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Formatting/ThemedJsonValueFormatter.cs b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Formatting/ThemedJsonValueFormatter.cs index 2294ea7..95c7d5c 100644 --- a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Formatting/ThemedJsonValueFormatter.cs +++ b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Formatting/ThemedJsonValueFormatter.cs @@ -40,7 +40,7 @@ public override ThemedValueFormatter SwitchTheme(ConsoleTheme theme) protected override int VisitScalarValue(ThemedValueFormatterState state, ScalarValue scalar) { - if (scalar == null) + if (scalar is null) throw new ArgumentNullException(nameof(scalar)); // At the top level, for scalar values, use "display" rendering. diff --git a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/ExceptionTokenRenderer.cs b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/ExceptionTokenRenderer.cs index 31aa4c2..eb8339f 100644 --- a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/ExceptionTokenRenderer.cs +++ b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/ExceptionTokenRenderer.cs @@ -34,7 +34,7 @@ public override void Render(LogEvent logEvent, TextWriter output) { // Padding is never applied by this renderer. - if (logEvent.Exception == null) + if (logEvent.Exception is null) return; var lines = new StringReader(logEvent.Exception.ToString()); diff --git a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/LevelOutputFormat.cs b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/LevelOutputFormat.cs index fdeefe2..d870705 100644 --- a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/LevelOutputFormat.cs +++ b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/LevelOutputFormat.cs @@ -57,7 +57,7 @@ static class LevelOutputFormat public static string GetLevelMoniker(LogEventLevel value, string format = null) { - if (format == null || format.Length != 2 && format.Length != 3) + if (format is null || format.Length != 2 && format.Length != 3) return Casing.Format(value.ToString(), format); // Using int.Parse() here requires allocating a string to exclude the first character prefix. diff --git a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/MessageTemplateOutputTokenRenderer.cs b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/MessageTemplateOutputTokenRenderer.cs index 6ca5fb4..90e81e0 100644 --- a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/MessageTemplateOutputTokenRenderer.cs +++ b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/MessageTemplateOutputTokenRenderer.cs @@ -54,7 +54,7 @@ public MessageTemplateOutputTokenRenderer(ConsoleTheme theme, PropertyToken toke public override void Render(LogEvent logEvent, TextWriter output) { - if (_token.Alignment == null || !_theme.CanBuffer) + if (_token.Alignment is null || !_theme.CanBuffer) { _renderer.Render(logEvent.MessageTemplate, logEvent.Properties, output); return; diff --git a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/OutputTemplateRenderer.cs b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/OutputTemplateRenderer.cs index c2ef206..60aaed5 100644 --- a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/OutputTemplateRenderer.cs +++ b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/OutputTemplateRenderer.cs @@ -29,7 +29,7 @@ class OutputTemplateRenderer : ITextFormatter public OutputTemplateRenderer(ConsoleTheme theme, string outputTemplate, IFormatProvider formatProvider) { - if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate)); + if (outputTemplate is null) throw new ArgumentNullException(nameof(outputTemplate)); var template = new MessageTemplateParser().Parse(outputTemplate); var renderers = new List(); @@ -77,8 +77,8 @@ public OutputTemplateRenderer(ConsoleTheme theme, string outputTemplate, IFormat public void Format(LogEvent logEvent, TextWriter output) { - if (logEvent == null) throw new ArgumentNullException(nameof(logEvent)); - if (output == null) throw new ArgumentNullException(nameof(output)); + if (logEvent is null) throw new ArgumentNullException(nameof(logEvent)); + if (output is null) throw new ArgumentNullException(nameof(output)); foreach (var renderer in _renderers) renderer.Render(logEvent, output); diff --git a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/PropertiesTokenRenderer.cs b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/PropertiesTokenRenderer.cs index 8bfe16e..62b84b7 100644 --- a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/PropertiesTokenRenderer.cs +++ b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/PropertiesTokenRenderer.cs @@ -61,7 +61,7 @@ public override void Render(LogEvent logEvent, TextWriter output) var value = new StructureValue(included); - if (_token.Alignment == null || !_theme.CanBuffer) + if (_token.Alignment is null || !_theme.CanBuffer) { _valueFormatter.Format(value, output, null); return; diff --git a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/TimestampTokenRenderer.cs b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/TimestampTokenRenderer.cs index 5eac5e2..671fb65 100644 --- a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/TimestampTokenRenderer.cs +++ b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/TimestampTokenRenderer.cs @@ -43,7 +43,7 @@ public override void Render(LogEvent logEvent, TextWriter output) var _ = 0; using (_theme.Apply(output, ConsoleThemeStyle.SecondaryText, ref _)) { - if (_token.Alignment == null) + if (_token.Alignment is null) { sv.Render(output, _token.Format, _formatProvider); } diff --git a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Rendering/Padding.cs b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Rendering/Padding.cs index cd98433..12d2595 100644 --- a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Rendering/Padding.cs +++ b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Rendering/Padding.cs @@ -29,7 +29,7 @@ static class Padding /// The alignment settings to apply when rendering . public static void Apply(TextWriter output, string value, Alignment? alignment) { - if (alignment == null || value.Length >= alignment.Value.Width) + if (alignment is null || value.Length >= alignment.Value.Width) { output.Write(value); return; diff --git a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/AnsiConsoleTheme.cs b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/AnsiConsoleTheme.cs index 3ff6ce8..292590a 100644 --- a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/AnsiConsoleTheme.cs +++ b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/AnsiConsoleTheme.cs @@ -49,7 +49,7 @@ public class AnsiConsoleTheme : ConsoleTheme /// Styles to apply within the theme. public AnsiConsoleTheme(IReadOnlyDictionary styles) { - if (styles == null) throw new ArgumentNullException(nameof(styles)); + if (styles is null) throw new ArgumentNullException(nameof(styles)); _styles = styles.ToDictionary(kv => kv.Key, kv => kv.Value); } diff --git a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/SystemConsoleTheme.cs b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/SystemConsoleTheme.cs index 5eaf7d8..bfec793 100644 --- a/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/SystemConsoleTheme.cs +++ b/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/SystemConsoleTheme.cs @@ -46,7 +46,7 @@ public class SystemConsoleTheme : ConsoleTheme /// Styles to apply within the theme. public SystemConsoleTheme(IReadOnlyDictionary styles) { - if (styles == null) throw new ArgumentNullException(nameof(styles)); + if (styles is null) throw new ArgumentNullException(nameof(styles)); Styles = styles.ToDictionary(kv => kv.Key, kv => kv.Value); } From 3e849e45c99c8f50b1ac3523ab29f79e05751fbc Mon Sep 17 00:00:00 2001 From: Rafael Cordeiro Date: Fri, 17 Jul 2020 22:47:52 -0700 Subject: [PATCH 2/2] Force SyncRoot be readonly --- .../ConsoleLoggerConfigurationExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Serilog.Sinks.Console/ConsoleLoggerConfigurationExtensions.cs b/src/Serilog.Sinks.Console/ConsoleLoggerConfigurationExtensions.cs index 2786dbd..390b174 100644 --- a/src/Serilog.Sinks.Console/ConsoleLoggerConfigurationExtensions.cs +++ b/src/Serilog.Sinks.Console/ConsoleLoggerConfigurationExtensions.cs @@ -28,7 +28,7 @@ namespace Serilog /// public static class ConsoleLoggerConfigurationExtensions { - static object DefaultSyncRoot = new object(); + static readonly object DefaultSyncRoot = new object(); const string DefaultConsoleOutputTemplate = "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"; ///