Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump .editorconfig & use file scoped namespaces #429

Merged
merged 1 commit into from
Nov 9, 2021
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
6 changes: 4 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Version: 3.0.0 (Using https://semver.org/)
# Updated: 2021-08-09
# Version: 4.0.0 (Using https://semver.org/)
# Updated: 2021-10-12
# See https://github.com/RehanSaeed/EditorConfig/releases for release notes.
# See https://github.com/RehanSaeed/EditorConfig for updates to this file.
# See http://EditorConfig.org for more information about .editorconfig files.
Expand Down Expand Up @@ -167,6 +167,8 @@ dotnet_diagnostic.IDE0063.severity = suggestion
csharp_using_directive_placement = inside_namespace:warning
# Modifier preferences
csharp_prefer_static_local_function = true:warning
# Undocumented
csharp_style_namespace_declarations = file_scoped:warning

##########################################
# Unnecessary Code Rules
Expand Down
53 changes: 26 additions & 27 deletions Benchmarks/Serilog.Exceptions.Benchmark/BenchmarkException.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
namespace Serilog.Exceptions.Benchmark
{
using System;
using System.Runtime.Serialization;
namespace Serilog.Exceptions.Benchmark;

using System;
using System.Runtime.Serialization;

[Serializable]
public class BenchmarkException : Exception
[Serializable]
public class BenchmarkException : Exception
{
public BenchmarkException()
{
public BenchmarkException()
{
}
}

public BenchmarkException(string message)
: base(message)
{
}
public BenchmarkException(string message)
: base(message)
{
}

public BenchmarkException(string message, Exception inner)
: base(message, inner)
{
}
public BenchmarkException(string message, Exception inner)
: base(message, inner)
{
}

protected BenchmarkException(
SerializationInfo info,
StreamingContext context)
: base(info, context)
{
}
protected BenchmarkException(
SerializationInfo info,
StreamingContext context)
: base(info, context)
{
}

public string? ParamString { get; set; }
public string? ParamString { get; set; }

public int ParamInt { get; set; }
public int ParamInt { get; set; }

public Point? Point { get; set; }
}
public Point? Point { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
namespace Serilog.Exceptions.Benchmark
namespace Serilog.Exceptions.Benchmark;

using System;
using System.Collections.Generic;
using Serilog.Exceptions.Core;
using Serilog.Exceptions.Destructurers;

/// <summary>
/// A destructurer used in benchmarks.
/// </summary>
/// <seealso cref="ExceptionDestructurer" />
public class BenchmarkExceptionDestructurer : ExceptionDestructurer
{
using System;
using System.Collections.Generic;
using Serilog.Exceptions.Core;
using Serilog.Exceptions.Destructurers;
/// <inheritdoc />
public override Type[] TargetTypes => new[] { typeof(BenchmarkException) };

/// <summary>
/// A destructurer used in benchmarks.
/// </summary>
/// <seealso cref="ExceptionDestructurer" />
public class BenchmarkExceptionDestructurer : ExceptionDestructurer
/// <inheritdoc />
public override void Destructure(
Exception exception,
IExceptionPropertiesBag propertiesBag,
Func<Exception, IReadOnlyDictionary<string, object?>?> destructureException)
{
/// <inheritdoc />
public override Type[] TargetTypes => new[] { typeof(BenchmarkException) };

/// <inheritdoc />
public override void Destructure(
Exception exception,
IExceptionPropertiesBag propertiesBag,
Func<Exception, IReadOnlyDictionary<string, object?>?> destructureException)
{
base.Destructure(exception, propertiesBag, destructureException);
base.Destructure(exception, propertiesBag, destructureException);

#pragma warning disable CA1062 // Validate arguments of public methods
var benchmarkException = (BenchmarkException)exception;
propertiesBag.AddProperty("ParamString", benchmarkException.ParamString);
propertiesBag.AddProperty("ParamInt", benchmarkException.ParamInt);
propertiesBag.AddProperty("Point", new Dictionary<string, object?>
var benchmarkException = (BenchmarkException)exception;
propertiesBag.AddProperty("ParamString", benchmarkException.ParamString);
propertiesBag.AddProperty("ParamInt", benchmarkException.ParamInt);
propertiesBag.AddProperty("Point", new Dictionary<string, object?>
{
{ "X", benchmarkException.Point?.X },
{ "Y", benchmarkException.Point?.Y },
});
#pragma warning restore CA1062 // Validate arguments of public methods
}
}
}
115 changes: 57 additions & 58 deletions Benchmarks/Serilog.Exceptions.Benchmark/DestructuringBenchmark.cs
Original file line number Diff line number Diff line change
@@ -1,74 +1,73 @@
namespace Serilog.Exceptions.Benchmark
namespace Serilog.Exceptions.Benchmark;

using System;
using System.Collections.Generic;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using Serilog.Exceptions.Destructurers;

[KeepBenchmarkFiles]
[MemoryDiagnoser]
[MinColumn]
[MaxColumn]
[HtmlExporter]
[CsvMeasurementsExporter]
[RPlotExporter]
[SimpleJob(RuntimeMoniker.Net60)]
[SimpleJob(RuntimeMoniker.Net472)]
public class DestructuringBenchmark
{
using System;
using System.Collections.Generic;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using Serilog.Exceptions.Destructurers;
private readonly ReflectionBasedDestructurer reflectionBasedDestructurer = new(10);
private readonly BenchmarkExceptionDestructurer benchmarkExceptionDestructurer = new();
private BenchmarkException benchmarkException = default!;

[KeepBenchmarkFiles]
[MemoryDiagnoser]
[MinColumn]
[MaxColumn]
[HtmlExporter]
[CsvMeasurementsExporter]
[RPlotExporter]
[SimpleJob(RuntimeMoniker.Net60)]
[SimpleJob(RuntimeMoniker.Net472)]
public class DestructuringBenchmark
[GlobalSetup]
public void Setup()
{
private readonly ReflectionBasedDestructurer reflectionBasedDestructurer = new(10);
private readonly BenchmarkExceptionDestructurer benchmarkExceptionDestructurer = new();
private BenchmarkException benchmarkException = default!;

[GlobalSetup]
public void Setup()
try
{
try
{
throw new BenchmarkException()
{
ParamInt = 123,
ParamString = "some param value",
Point = new Point() { X = 666, Y = 777 },
};
}
catch (BenchmarkException ex)
throw new BenchmarkException()
{
this.benchmarkException = ex;
}
ParamInt = 123,
ParamString = "some param value",
Point = new Point() { X = 666, Y = 777 },
};
}

public IReadOnlyDictionary<string, object?> DestructureUsingReflectionDestructurer(Exception ex)
catch (BenchmarkException ex)
{
var bag = new ExceptionPropertiesBag(ex);
this.benchmarkException = ex;
}
}

this.reflectionBasedDestructurer.Destructure(
ex,
bag,
_ => new Dictionary<string, object?>());
public IReadOnlyDictionary<string, object?> DestructureUsingReflectionDestructurer(Exception ex)
{
var bag = new ExceptionPropertiesBag(ex);

return bag.GetResultDictionary();
}
this.reflectionBasedDestructurer.Destructure(
ex,
bag,
_ => new Dictionary<string, object?>());

[Benchmark]
public IReadOnlyDictionary<string, object?> ReflectionDestructurer() =>
this.DestructureUsingReflectionDestructurer(this.benchmarkException);
return bag.GetResultDictionary();
}

public IReadOnlyDictionary<string, object?> DestructureUsingCustomDestructurer(Exception ex)
{
var bag = new ExceptionPropertiesBag(ex);
[Benchmark]
public IReadOnlyDictionary<string, object?> ReflectionDestructurer() =>
this.DestructureUsingReflectionDestructurer(this.benchmarkException);

this.benchmarkExceptionDestructurer.Destructure(
ex,
bag,
_ => new Dictionary<string, object?>());
public IReadOnlyDictionary<string, object?> DestructureUsingCustomDestructurer(Exception ex)
{
var bag = new ExceptionPropertiesBag(ex);

return bag.GetResultDictionary();
}
this.benchmarkExceptionDestructurer.Destructure(
ex,
bag,
_ => new Dictionary<string, object?>());

[Benchmark]
public IReadOnlyDictionary<string, object?> CustomDestructurer() =>
this.DestructureUsingCustomDestructurer(this.benchmarkException);
return bag.GetResultDictionary();
}

[Benchmark]
public IReadOnlyDictionary<string, object?> CustomDestructurer() =>
this.DestructureUsingCustomDestructurer(this.benchmarkException);
}
83 changes: 41 additions & 42 deletions Benchmarks/Serilog.Exceptions.Benchmark/ExceptionPropertiesBag.cs
Original file line number Diff line number Diff line change
@@ -1,60 +1,59 @@
namespace Serilog.Exceptions.Benchmark
namespace Serilog.Exceptions.Benchmark;

using System;
using System.Collections.Generic;
using Serilog.Exceptions.Core;
using Serilog.Exceptions.Filters;

internal class ExceptionPropertiesBag : IExceptionPropertiesBag
{
using System;
using System.Collections.Generic;
using Serilog.Exceptions.Core;
using Serilog.Exceptions.Filters;
private readonly Exception exception;
private readonly IExceptionPropertyFilter? filter;
private readonly Dictionary<string, object?> properties = new();

// We keep a note on whether the results were collected to be sure that
// after that there are no changes. This is the application of fail-fast principle.
private bool resultsCollected;

internal class ExceptionPropertiesBag : IExceptionPropertiesBag
public ExceptionPropertiesBag(Exception exception, IExceptionPropertyFilter? filter = null)
{
private readonly Exception exception;
private readonly IExceptionPropertyFilter? filter;
private readonly Dictionary<string, object?> properties = new();
this.exception = exception ?? throw new ArgumentNullException(nameof(exception));
this.filter = filter;
}

// We keep a note on whether the results were collected to be sure that
// after that there are no changes. This is the application of fail-fast principle.
private bool resultsCollected;
public IReadOnlyDictionary<string, object?> GetResultDictionary()
{
this.resultsCollected = true;
return this.properties;
}

public ExceptionPropertiesBag(Exception exception, IExceptionPropertyFilter? filter = null)
public void AddProperty(string key, object? value)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(key);
#else
if (key is null)
{
this.exception = exception ?? throw new ArgumentNullException(nameof(exception));
this.filter = filter;
throw new ArgumentNullException(nameof(key));
}
#endif

public IReadOnlyDictionary<string, object?> GetResultDictionary()
if (this.resultsCollected)
{
this.resultsCollected = true;
return this.properties;
throw new InvalidOperationException(
$"Cannot add exception property '{key}' to bag, after results were already collected");
}

public void AddProperty(string key, object? value)
if (this.filter is not null)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(key);
#else
if (key is null)
{
throw new ArgumentNullException(nameof(key));
}
#endif

if (this.resultsCollected)
if (this.filter.ShouldPropertyBeFiltered(this.exception, key, value))
{
throw new InvalidOperationException(
$"Cannot add exception property '{key}' to bag, after results were already collected");
return;
}

if (this.filter is not null)
{
if (this.filter.ShouldPropertyBeFiltered(this.exception, key, value))
{
return;
}
}

this.properties.Add(key, value);
}

public bool ContainsProperty(string key) => this.properties.ContainsKey(key);
this.properties.Add(key, value);
}

public bool ContainsProperty(string key) => this.properties.ContainsKey(key);
}
15 changes: 7 additions & 8 deletions Benchmarks/Serilog.Exceptions.Benchmark/Point.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
namespace Serilog.Exceptions.Benchmark
namespace Serilog.Exceptions.Benchmark;

public class Point
{
public class Point
{
public Point() => this.Z = 3;
public Point() => this.Z = 3;

public int X { get; set; }
public int X { get; set; }

public int Y { get; set; }
public int Y { get; set; }

#pragma warning disable IDE0052 // Remove unread private members
private int Z { get; set; }
private int Z { get; set; }
#pragma warning restore IDE0052 // Remove unread private members
}
}
Loading