Skip to content

Commit

Permalink
remove some redundant variables
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored and nblumhardt committed May 5, 2021
1 parent 34bd212 commit 9669a3f
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 63 deletions.
22 changes: 11 additions & 11 deletions src/Serilog/Formatting/Json/JsonFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013-2015 Serilog Contributors
// Copyright 2013-2015 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -86,8 +86,8 @@ public class JsonFormatter : ITextFormatter

_literalWriters = new Dictionary<Type, Action<object, bool, TextWriter>>
{
{ typeof(bool), (v, q, w) => WriteBoolean((bool)v, w) },
{ typeof(char), (v, q, w) => WriteString(((char)v).ToString(), w) },
{ typeof(bool), (v, _, w) => WriteBoolean((bool)v, w) },
{ typeof(char), (v, _, w) => WriteString(((char)v).ToString(), w) },
{ typeof(byte), WriteToString },
{ typeof(sbyte), WriteToString },
{ typeof(short), WriteToString },
Expand All @@ -96,16 +96,16 @@ public class JsonFormatter : ITextFormatter
{ typeof(uint), WriteToString },
{ typeof(long), WriteToString },
{ typeof(ulong), WriteToString },
{ typeof(float), (v, q, w) => WriteSingle((float)v, w) },
{ typeof(double), (v, q, w) => WriteDouble((double)v, w) },
{ typeof(float), (v, _, w) => WriteSingle((float)v, w) },
{ typeof(double), (v, _, w) => WriteDouble((double)v, w) },
{ typeof(decimal), WriteToString },
{ typeof(string), (v, q, w) => WriteString((string)v, w) },
{ typeof(DateTime), (v, q, w) => WriteDateTime((DateTime)v, w) },
{ typeof(DateTimeOffset), (v, q, w) => WriteOffset((DateTimeOffset)v, w) },
{ typeof(string), (v, _, w) => WriteString((string)v, w) },
{ typeof(DateTime), (v, _, w) => WriteDateTime((DateTime)v, w) },
{ typeof(DateTimeOffset), (v, _, w) => WriteOffset((DateTimeOffset)v, w) },
{ typeof(ScalarValue), (v, q, w) => WriteLiteral(((ScalarValue)v).Value, w, q) },
{ typeof(SequenceValue), (v, q, w) => WriteSequence(((SequenceValue)v).Elements, w) },
{ typeof(DictionaryValue), (v, q, w) => WriteDictionary(((DictionaryValue)v).Elements, w) },
{ typeof(StructureValue), (v, q, w) => WriteStructure(((StructureValue)v).TypeTag, ((StructureValue)v).Properties, w) },
{ typeof(SequenceValue), (v, _, w) => WriteSequence(((SequenceValue)v).Elements, w) },
{ typeof(DictionaryValue), (v, _, w) => WriteDictionary(((DictionaryValue)v).Elements, w) },
{ typeof(StructureValue), (v, _, w) => WriteStructure(((StructureValue)v).TypeTag, ((StructureValue)v).Properties, w) },
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -28,7 +28,7 @@ public class MessageTemplateCacheBenchmark_Cached
[GlobalSetup]
public void Setup()
{
_templateList = Enumerable.Range(0, Items).Select(x => $"{DefaultOutputTemplate}_{Guid.NewGuid()}").ToList();
_templateList = Enumerable.Range(0, Items).Select(_ => $"{DefaultOutputTemplate}_{Guid.NewGuid()}").ToList();

_concurrentCache = new ConcurrentDictionaryMessageTemplateCache(NoOpMessageTemplateParser.Instance);
_dictionaryCache = new DictionaryMessageTemplateCache(NoOpMessageTemplateParser.Instance);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Attributes;
using Serilog.Core;
using Serilog.Core.Pipeline;
using Serilog.PerformanceTests.Support;
Expand Down Expand Up @@ -28,7 +28,7 @@ public class MessageTemplateCacheBenchmark_Leaking
[GlobalSetup]
public void Setup()
{
_templateList = Enumerable.Range(0, Items).Select(x => $"{DefaultOutputTemplate}_{Guid.NewGuid()}").ToList();
_templateList = Enumerable.Range(0, Items).Select(_ => $"{DefaultOutputTemplate}_{Guid.NewGuid()}").ToList();
}

[Benchmark(Baseline = true)]
Expand Down
14 changes: 7 additions & 7 deletions test/Serilog.Tests/Core/AuditSinkTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Reflection;
using Serilog.Tests.Support;
using Xunit;
Expand All @@ -11,7 +11,7 @@ public class AuditSinkTests
public void ExceptionsThrownByAuditSinksArePropagated()
{
var logger = new LoggerConfiguration()
.AuditTo.Sink(new DelegatingSink(e => { throw new Exception("Boom!"); }))
.AuditTo.Sink(new DelegatingSink(_ => { throw new Exception("Boom!"); }))
.CreateLogger();

Assert.Throws<AggregateException>(() => logger.Write(Some.InformationEvent()));
Expand All @@ -21,8 +21,8 @@ public void ExceptionsThrownByAuditSinksArePropagated()
public void ExceptionsThrownByFiltersArePropagatedIfAuditingEnabled()
{
var logger = new LoggerConfiguration()
.AuditTo.Sink(new DelegatingSink(e => { }))
.Filter.ByExcluding(e => { throw new Exception("Boom!"); })
.AuditTo.Sink(new DelegatingSink(_ => { }))
.Filter.ByExcluding(_ => { throw new Exception("Boom!"); })
.CreateLogger();

Assert.Throws<Exception>(() => logger.Write(Some.InformationEvent()));
Expand All @@ -32,7 +32,7 @@ public void ExceptionsThrownByFiltersArePropagatedIfAuditingEnabled()
public void ExceptionsThrownByAuditSinksArePropagatedFromChildLoggers()
{
var logger = new LoggerConfiguration()
.AuditTo.Sink(new DelegatingSink(e => { throw new Exception("Boom!"); }))
.AuditTo.Sink(new DelegatingSink(_ => { throw new Exception("Boom!"); }))
.CreateLogger();

Assert.Throws<AggregateException>(() => logger
Expand All @@ -45,7 +45,7 @@ public void ExceptionsThrownByDestructuringPoliciesArePropagatedIfAuditingEnable
{
var logger = new LoggerConfiguration()
.AuditTo.Sink(new CollectingSink())
.Destructure.ByTransforming<Value>(v => { throw new Exception("Boom!"); })
.Destructure.ByTransforming<Value>(_ => { throw new Exception("Boom!"); })
.CreateLogger();

Assert.Throws<Exception>(() => logger.Information("{@Value}", new Value()));
Expand Down Expand Up @@ -97,4 +97,4 @@ public string Property
}
}
}
}
}
2 changes: 1 addition & 1 deletion test/Serilog.Tests/Core/LoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void AnExceptionThrownByAnEnricherIsNotPropagated(Type loggerType)

var l = CreateLogger(loggerType, lc => lc
.WriteTo.Sink(new StringSink())
.Enrich.With(new DelegatingEnricher((le, pf) =>
.Enrich.With(new DelegatingEnricher((_, _) =>
{
thrown = true;
throw new Exception("No go, pal.");
Expand Down
8 changes: 4 additions & 4 deletions test/Serilog.Tests/Core/MessageTemplateTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Serilog.Capturing;
using Serilog.Capturing;
using Serilog.Core;
using System;
using System.Globalization;
Expand Down Expand Up @@ -193,7 +193,7 @@ public void AnonymousTypeShouldBeRendered()
public void EnumerableOfAnonymousTypeShouldBeRendered()
{
var anonymous = new { Foo = 4M, Bar = "Baz" };
var enumerable = Enumerable.Repeat("MyKey", 1).Select(v => anonymous);
var enumerable = Enumerable.Repeat("MyKey", 1).Select(_ => anonymous);
var m = Render("Enumerable with anonymous type {enumerable}", enumerable);
Assert.Equal("Enumerable with anonymous type [\"{ Foo = 4, Bar = Baz }\"]", m);
}
Expand All @@ -202,7 +202,7 @@ public void EnumerableOfAnonymousTypeShouldBeRendered()
public void DictionaryOfAnonymousTypeAsValueShouldBeRendered()
{
var anonymous = new { Test = 5M };
var dictionary = Enumerable.Repeat("MyKey", 1).ToDictionary(v => v, v => anonymous);
var dictionary = Enumerable.Repeat("MyKey", 1).ToDictionary(v => v, _ => anonymous);
var m = Render("Dictionary with anonymous type value {dictionary}", dictionary);
Assert.Equal("Dictionary with anonymous type value [(\"MyKey\": \"{ Test = 5 }\")]", m);
}
Expand All @@ -211,7 +211,7 @@ public void DictionaryOfAnonymousTypeAsValueShouldBeRendered()
public void DictionaryOfAnonymousTypeAsKeyShouldBeRendered()
{
var anonymous = new { Bar = 6M, Baz = 4M };
var dictionary = Enumerable.Repeat("MyValue", 1).ToDictionary(v => anonymous, v => v);
var dictionary = Enumerable.Repeat("MyValue", 1).ToDictionary(_ => anonymous, v => v);
var m = Render("Dictionary with anonymous type key {dictionary}", dictionary);
Assert.Equal("Dictionary with anonymous type key [\"[{ Bar = 6, Baz = 4 }, MyValue]\"]", m);
}
Expand Down
10 changes: 5 additions & 5 deletions test/Serilog.Tests/Core/SafeAggregateSinkTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Serilog.Core.Sinks;
using Serilog.Core.Sinks;
using Serilog.Tests.Support;
using System;
using Xunit;
Expand All @@ -12,7 +12,7 @@ public void AnExceptionThrownByASinkIsNotPropagated()
{
var thrown = false;

var s = new SafeAggregateSink(new[] { new DelegatingSink(le => {
var s = new SafeAggregateSink(new[] { new DelegatingSink(_ => {
thrown = true;
throw new Exception("No go, pal.");
}) });
Expand All @@ -28,9 +28,9 @@ public void WhenASinkThrowsOtherSinksAreStillInvoked()
bool called1 = false, called2 = false;

var s = new SafeAggregateSink(new[] {
new DelegatingSink(le => called1 = true),
new DelegatingSink(le => { throw new Exception("No go, pal."); }),
new DelegatingSink(le => called2 = true)
new DelegatingSink(_ => called1 = true),
new DelegatingSink(_ => { throw new Exception("No go, pal."); }),
new DelegatingSink(_ => called2 = true)
});

s.Emit(Some.InformationEvent());
Expand Down
8 changes: 4 additions & 4 deletions test/Serilog.Tests/Filters/MatchingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void EventsCanBeExcludedBySource()

var log = new LoggerConfiguration()
.Filter.ByExcluding(Matching.FromSource<MatchingTests>())
.WriteTo.Sink(new DelegatingSink(e => written = true))
.WriteTo.Sink(new DelegatingSink(_ => written = true))
.CreateLogger();

var sourceContext = log.ForContext<MatchingTests>();
Expand All @@ -29,7 +29,7 @@ public void EventsCanBeExcludedByPredicate()

var log = new LoggerConfiguration()
.Filter.ByExcluding(Matching.WithProperty<int>("Count", p => p < 10))
.WriteTo.Sink(new DelegatingSink(e => seen++))
.WriteTo.Sink(new DelegatingSink(_ => seen++))
.CreateLogger();

log.Warning("Unrelated");
Expand All @@ -46,7 +46,7 @@ public void SourceFiltersWorkOnNamespaces()
var written = false;
var log = new LoggerConfiguration()
.Filter.ByExcluding(Matching.FromSource("Serilog.Tests"))
.WriteTo.Sink(new DelegatingSink(e => written = true))
.WriteTo.Sink(new DelegatingSink(_ => written = true))
.CreateLogger()
.ForContext<MatchingTests>();

Expand All @@ -60,7 +60,7 @@ public void SourceFiltersSkipNonNamespaces()
var written = false;
var log = new LoggerConfiguration()
.Filter.ByExcluding(Matching.FromSource("Serilog.Tests"))
.WriteTo.Sink(new DelegatingSink(e => written = true))
.WriteTo.Sink(new DelegatingSink(_ => written = true))
.CreateLogger()
.ForContext(Serilog.Core.Constants.SourceContextPropertyName, "Serilog.TestsLong");

Expand Down
18 changes: 9 additions & 9 deletions test/Serilog.Tests/LoggerConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public void WritingToALoggerWritesToSubLogger()

var logger = new LoggerConfiguration()
.WriteTo.Logger(l => l
.WriteTo.Sink(new DelegatingSink(e => eventReceived = true)))
.WriteTo.Sink(new DelegatingSink(_ => eventReceived = true)))
.CreateLogger();

logger.Write(Some.InformationEvent());
Expand All @@ -241,7 +241,7 @@ public void SubLoggerRestrictsFilter()
var logger = new LoggerConfiguration()
.WriteTo.Logger(l => l
.MinimumLevel.Fatal()
.WriteTo.Sink(new DelegatingSink(e => eventReceived = true)))
.WriteTo.Sink(new DelegatingSink(_ => eventReceived = true)))
.CreateLogger();

logger.Write(Some.InformationEvent());
Expand All @@ -257,8 +257,8 @@ public void EnrichersExecuteInConfigurationOrder()

var logger = new LoggerConfiguration()
.WriteTo.Sink(new StringSink())
.Enrich.With(new DelegatingEnricher((e, f) => e.AddPropertyIfAbsent(property)))
.Enrich.With(new DelegatingEnricher((e, f) => enrichedPropertySeen = e.Properties.ContainsKey(property.Name)))
.Enrich.With(new DelegatingEnricher((e, _) => e.AddPropertyIfAbsent(property)))
.Enrich.With(new DelegatingEnricher((e, _) => enrichedPropertySeen = e.Properties.ContainsKey(property.Name)))
.CreateLogger();

logger.Write(Some.InformationEvent());
Expand Down Expand Up @@ -483,7 +483,7 @@ public void DynamicallySwitchingSinkRestrictsOutput()

var logger = new LoggerConfiguration()
.WriteTo.Sink(
new DelegatingSink(e => eventsReceived++),
new DelegatingSink(_ => eventsReceived++),
levelSwitch: levelSwitch)
.CreateLogger();

Expand Down Expand Up @@ -566,7 +566,7 @@ public void LowerMinimumLevelOverridesArePropagated()
public void ExceptionsThrownBySinksAreNotPropagated()
{
var logger = new LoggerConfiguration()
.WriteTo.Sink(new DelegatingSink(e => throw new Exception("Boom!")))
.WriteTo.Sink(new DelegatingSink(_ => throw new Exception("Boom!")))
.CreateLogger();

logger.Write(Some.InformationEvent());
Expand All @@ -579,7 +579,7 @@ public void ExceptionsThrownBySinksAreNotPropagatedEvenWhenAuditingIsPresent()
{
var logger = new LoggerConfiguration()
.AuditTo.Sink(new CollectingSink())
.WriteTo.Sink(new DelegatingSink(e => throw new Exception("Boom!")))
.WriteTo.Sink(new DelegatingSink(_ => throw new Exception("Boom!")))
.CreateLogger();

logger.Write(Some.InformationEvent());
Expand All @@ -591,7 +591,7 @@ public void ExceptionsThrownBySinksAreNotPropagatedEvenWhenAuditingIsPresent()
public void ExceptionsThrownByFiltersAreNotPropagated()
{
var logger = new LoggerConfiguration()
.Filter.ByExcluding(e => throw new Exception("Boom!"))
.Filter.ByExcluding(_ => throw new Exception("Boom!"))
.CreateLogger();

logger.Write(Some.InformationEvent());
Expand All @@ -606,7 +606,7 @@ public void ExceptionsThrownByDestructuringPoliciesAreNotPropagated()
{
var logger = new LoggerConfiguration()
.WriteTo.Sink(new CollectingSink())
.Destructure.ByTransforming<Value>(v => throw new Exception("Boom!"))
.Destructure.ByTransforming<Value>(_ => throw new Exception("Boom!"))
.CreateLogger();

logger.Information("{@Value}", new Value());
Expand Down
20 changes: 2 additions & 18 deletions test/TestDummies/DummyLoggerConfigurationExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using Serilog;
using Serilog;
using Serilog.Configuration;
using Serilog.Core;
using Serilog.Events;
using Serilog.Formatting;
using System;
using TestDummies.Console;
using TestDummies.Console.Themes;
Expand All @@ -18,29 +17,14 @@ public static LoggerConfiguration WithDummyThreadId(this LoggerEnrichmentConfigu

public static LoggerConfiguration DummyRollingFile(
this LoggerSinkConfiguration loggerSinkConfiguration,
string pathFormat,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
string outputTemplate = null,
IFormatProvider formatProvider = null)
{
return loggerSinkConfiguration.Sink(new DummyRollingFileSink(), restrictedToMinimumLevel);
}

public static LoggerConfiguration DummyRollingFile(
this LoggerSinkConfiguration loggerSinkConfiguration,
ITextFormatter formatter,
string pathFormat,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum)
{
return loggerSinkConfiguration.Sink(new DummyRollingFileSink(), restrictedToMinimumLevel);
}

public static LoggerConfiguration DummyRollingFile(
this LoggerAuditSinkConfiguration loggerSinkConfiguration,
string pathFormat,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
string outputTemplate = null,
IFormatProvider formatProvider = null)
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum)
{
return loggerSinkConfiguration.Sink(new DummyRollingFileAuditSink(), restrictedToMinimumLevel);
}
Expand Down

0 comments on commit 9669a3f

Please sign in to comment.