Skip to content

Commit e992708

Browse files
authored
Merge pull request #96 from rafaelsc/optimization/fastNullCheck
Optimization - Use faster null checking
2 parents eafe7d5 + 3e849e4 commit e992708

13 files changed

+21
-21
lines changed

src/Serilog.Sinks.Console/ConsoleLoggerConfigurationExtensions.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace Serilog
2828
/// </summary>
2929
public static class ConsoleLoggerConfigurationExtensions
3030
{
31-
static object DefaultSyncRoot = new object();
31+
static readonly object DefaultSyncRoot = new object();
3232
const string DefaultConsoleOutputTemplate = "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}";
3333

3434
/// <summary>
@@ -61,8 +61,8 @@ public static LoggerConfiguration Console(
6161
bool applyThemeToRedirectedOutput = false,
6262
object syncRoot = null)
6363
{
64-
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
65-
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));
64+
if (sinkConfiguration is null) throw new ArgumentNullException(nameof(sinkConfiguration));
65+
if (outputTemplate is null) throw new ArgumentNullException(nameof(outputTemplate));
6666

6767
var appliedTheme = !applyThemeToRedirectedOutput && (System.Console.IsOutputRedirected || System.Console.IsErrorRedirected) ?
6868
ConsoleTheme.None :
@@ -97,8 +97,8 @@ public static LoggerConfiguration Console(
9797
LogEventLevel? standardErrorFromLevel = null,
9898
object syncRoot = null)
9999
{
100-
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
101-
if (formatter == null) throw new ArgumentNullException(nameof(formatter));
100+
if (sinkConfiguration is null) throw new ArgumentNullException(nameof(sinkConfiguration));
101+
if (formatter is null) throw new ArgumentNullException(nameof(formatter));
102102

103103
syncRoot = syncRoot ?? DefaultSyncRoot;
104104
return sinkConfiguration.Sink(new ConsoleSink(ConsoleTheme.None, formatter, standardErrorFromLevel, syncRoot), restrictedToMinimumLevel, levelSwitch);

src/Serilog.Sinks.Console/Sinks/SystemConsole/ConsoleSink.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void Emit(LogEvent logEvent)
7979

8080
TextWriter SelectOutputStream(LogEventLevel logEventLevel)
8181
{
82-
if (_standardErrorFromLevel == null)
82+
if (_standardErrorFromLevel is null)
8383
return Console.Out;
8484

8585
return logEventLevel < _standardErrorFromLevel ? Console.Out : Console.Error;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ public override ThemedValueFormatter SwitchTheme(ConsoleTheme theme)
3737

3838
protected override int VisitScalarValue(ThemedValueFormatterState state, ScalarValue scalar)
3939
{
40-
if (scalar == null)
40+
if (scalar is null)
4141
throw new ArgumentNullException(nameof(scalar));
4242
return FormatLiteralValue(scalar, state.Output, state.Format);
4343
}
4444

4545
protected override int VisitSequenceValue(ThemedValueFormatterState state, SequenceValue sequence)
4646
{
47-
if (sequence == null)
47+
if (sequence is null)
4848
throw new ArgumentNullException(nameof(sequence));
4949

5050
var count = 0;
@@ -155,7 +155,7 @@ public int FormatLiteralValue(ScalarValue scalar, TextWriter output, string form
155155
var value = scalar.Value;
156156
var count = 0;
157157

158-
if (value == null)
158+
if (value is null)
159159
{
160160
using (ApplyStyle(output, ConsoleThemeStyle.Null, ref count))
161161
output.Write("null");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public override ThemedValueFormatter SwitchTheme(ConsoleTheme theme)
4040

4141
protected override int VisitScalarValue(ThemedValueFormatterState state, ScalarValue scalar)
4242
{
43-
if (scalar == null)
43+
if (scalar is null)
4444
throw new ArgumentNullException(nameof(scalar));
4545

4646
// At the top level, for scalar values, use "display" rendering.

src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/ExceptionTokenRenderer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public override void Render(LogEvent logEvent, TextWriter output)
3434
{
3535
// Padding is never applied by this renderer.
3636

37-
if (logEvent.Exception == null)
37+
if (logEvent.Exception is null)
3838
return;
3939

4040
var lines = new StringReader(logEvent.Exception.ToString());

src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/LevelOutputFormat.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static class LevelOutputFormat
5757

5858
public static string GetLevelMoniker(LogEventLevel value, string format = null)
5959
{
60-
if (format == null || format.Length != 2 && format.Length != 3)
60+
if (format is null || format.Length != 2 && format.Length != 3)
6161
return Casing.Format(value.ToString(), format);
6262

6363
// Using int.Parse() here requires allocating a string to exclude the first character prefix.

src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/MessageTemplateOutputTokenRenderer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public MessageTemplateOutputTokenRenderer(ConsoleTheme theme, PropertyToken toke
5454

5555
public override void Render(LogEvent logEvent, TextWriter output)
5656
{
57-
if (_token.Alignment == null || !_theme.CanBuffer)
57+
if (_token.Alignment is null || !_theme.CanBuffer)
5858
{
5959
_renderer.Render(logEvent.MessageTemplate, logEvent.Properties, output);
6060
return;

src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/OutputTemplateRenderer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class OutputTemplateRenderer : ITextFormatter
2929

3030
public OutputTemplateRenderer(ConsoleTheme theme, string outputTemplate, IFormatProvider formatProvider)
3131
{
32-
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));
32+
if (outputTemplate is null) throw new ArgumentNullException(nameof(outputTemplate));
3333
var template = new MessageTemplateParser().Parse(outputTemplate);
3434

3535
var renderers = new List<OutputTemplateTokenRenderer>();
@@ -77,8 +77,8 @@ public OutputTemplateRenderer(ConsoleTheme theme, string outputTemplate, IFormat
7777

7878
public void Format(LogEvent logEvent, TextWriter output)
7979
{
80-
if (logEvent == null) throw new ArgumentNullException(nameof(logEvent));
81-
if (output == null) throw new ArgumentNullException(nameof(output));
80+
if (logEvent is null) throw new ArgumentNullException(nameof(logEvent));
81+
if (output is null) throw new ArgumentNullException(nameof(output));
8282

8383
foreach (var renderer in _renderers)
8484
renderer.Render(logEvent, output);

src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/PropertiesTokenRenderer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public override void Render(LogEvent logEvent, TextWriter output)
6161

6262
var value = new StructureValue(included);
6363

64-
if (_token.Alignment == null || !_theme.CanBuffer)
64+
if (_token.Alignment is null || !_theme.CanBuffer)
6565
{
6666
_valueFormatter.Format(value, output, null);
6767
return;

src/Serilog.Sinks.Console/Sinks/SystemConsole/Output/TimestampTokenRenderer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public override void Render(LogEvent logEvent, TextWriter output)
4343
var _ = 0;
4444
using (_theme.Apply(output, ConsoleThemeStyle.SecondaryText, ref _))
4545
{
46-
if (_token.Alignment == null)
46+
if (_token.Alignment is null)
4747
{
4848
sv.Render(output, _token.Format, _formatProvider);
4949
}

0 commit comments

Comments
 (0)