Skip to content

Commit

Permalink
Enable C# 8 nullable reference types
Browse files Browse the repository at this point in the history
  • Loading branch information
RehanSaeed committed Mar 12, 2021
1 parent d418a8c commit 2945cee
Show file tree
Hide file tree
Showing 34 changed files with 235 additions and 246 deletions.
4 changes: 2 additions & 2 deletions Benchmarks/Serilog.Exceptions.Benchmark/BenchmarkException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ protected BenchmarkException(
{
}

public string ParamString { get; set; }
public string? ParamString { 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
Expand Up @@ -18,18 +18,18 @@ public class BenchmarkExceptionDestructurer : ExceptionDestructurer
public override void Destructure(
Exception exception,
IExceptionPropertiesBag propertiesBag,
Func<Exception, IReadOnlyDictionary<string, object>> destructureException)
Func<Exception, IReadOnlyDictionary<string, object?>?> 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>
propertiesBag.AddProperty("Point", new Dictionary<string, object?>
{
{ "X", benchmarkException.Point.X },
{ "Y", benchmarkException.Point.Y },
{ "X", benchmarkException.Point?.X },
{ "Y", benchmarkException.Point?.Y },
});
#pragma warning restore CA1062 // Validate arguments of public methods
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class DestructuringBenchmark
{
private readonly ReflectionBasedDestructurer reflectionBasedDestructurer = new(10);
private readonly BenchmarkExceptionDestructurer benchmarkExceptionDestructurer = new();
private BenchmarkException benchmarkException;
private BenchmarkException benchmarkException = default!;

[GlobalSetup]
public void Setup()
Expand All @@ -39,36 +39,36 @@ public void Setup()
}
}

public IReadOnlyDictionary<string, object> DestructureUsingReflectionDestructurer(Exception ex)
public IReadOnlyDictionary<string, object?> DestructureUsingReflectionDestructurer(Exception ex)
{
var bag = new ExceptionPropertiesBag(ex);

this.reflectionBasedDestructurer.Destructure(
ex,
bag,
null);
null!);

return bag.GetResultDictionary();
}

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

public IReadOnlyDictionary<string, object> DestructureUsingCustomDestructurer(Exception ex)
public IReadOnlyDictionary<string, object?> DestructureUsingCustomDestructurer(Exception ex)
{
var bag = new ExceptionPropertiesBag(ex);

this.benchmarkExceptionDestructurer.Destructure(
ex,
bag,
null);
null!);

return bag.GetResultDictionary();
}

[Benchmark]
public IReadOnlyDictionary<string, object> CustomDestructurer() =>
public IReadOnlyDictionary<string, object?> CustomDestructurer() =>
this.DestructureUsingCustomDestructurer(this.benchmarkException);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@ namespace Serilog.Exceptions.Benchmark
internal class ExceptionPropertiesBag : IExceptionPropertiesBag
{
private readonly Exception exception;
private readonly IExceptionPropertyFilter filter;
private readonly Dictionary<string, object> properties = new();
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;

public ExceptionPropertiesBag(Exception exception, IExceptionPropertyFilter filter = null)
public ExceptionPropertiesBag(Exception exception, IExceptionPropertyFilter? filter = null)
{
this.exception = exception ?? throw new ArgumentNullException(nameof(exception));
this.filter = filter;
}

public IReadOnlyDictionary<string, object> GetResultDictionary()
public IReadOnlyDictionary<string, object?> GetResultDictionary()
{
this.resultsCollected = true;
return this.properties;
}

public void AddProperty(string key, object value)
public void AddProperty(string key, object? value)
{
if (key is null)
{
Expand Down
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<LangVersion>latest</LangVersion>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<AnalysisLevel>latest</AnalysisLevel>
<Nullable>enable</Nullable>
<NeutralLanguage>en-GB</NeutralLanguage>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class DbUpdateExceptionDestructurer : ExceptionDestructurer
public override void Destructure(
Exception exception,
IExceptionPropertiesBag propertiesBag,
Func<Exception, IReadOnlyDictionary<string, object>> destructureException)
Func<Exception, IReadOnlyDictionary<string, object?>?> destructureException)
{
base.Destructure(exception, propertiesBag, destructureException);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class SqlExceptionDestructurer : ExceptionDestructurer
public override void Destructure(
Exception exception,
IExceptionPropertiesBag propertiesBag,
Func<Exception, IReadOnlyDictionary<string, object>> destructureException)
Func<Exception, IReadOnlyDictionary<string, object?>?> destructureException)
{
base.Destructure(exception, propertiesBag, destructureException);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class SqlExceptionDestructurer : ExceptionDestructurer
public override void Destructure(
Exception exception,
IExceptionPropertiesBag propertiesBag,
Func<Exception, IReadOnlyDictionary<string, object>> destructureException)
Func<Exception, IReadOnlyDictionary<string, object?>?> destructureException)
{
base.Destructure(exception, propertiesBag, destructureException);

Expand All @@ -36,4 +36,4 @@ public override void Destructure(
#pragma warning restore CA1062 // Validate arguments of public methods
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class DestructuringOptionsBuilder : IDestructuringOptions
/// <summary>
/// Gets a global filter, that will be applied to every destructured property just before it is added to the result.
/// </summary>
public IExceptionPropertyFilter Filter { get; private set; }
public IExceptionPropertyFilter? Filter { get; private set; }

/// <summary>
/// Accumulates destructurers to be used by <see cref="ExceptionEnricher"/>.
Expand Down
2 changes: 1 addition & 1 deletion Source/Serilog.Exceptions/Core/ExceptionEnricher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
}
}

private IReadOnlyDictionary<string, object> DestructureException(Exception exception)
private IReadOnlyDictionary<string, object?>? DestructureException(Exception exception)
{
var exceptionType = exception.GetType();

Expand Down
10 changes: 5 additions & 5 deletions Source/Serilog.Exceptions/Core/ExceptionPropertiesBag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace Serilog.Exceptions.Core
internal class ExceptionPropertiesBag : IExceptionPropertiesBag
{
private readonly Exception exception;
private readonly IExceptionPropertyFilter filter;
private readonly Dictionary<string, object> properties = new();
private readonly IExceptionPropertyFilter? filter;
private readonly Dictionary<string, object?> properties = new();

/// <summary>
/// We keep a note on whether the results were collected to be sure that after that there are no changes. This
Expand All @@ -22,21 +22,21 @@ internal class ExceptionPropertiesBag : IExceptionPropertiesBag
/// </summary>
/// <param name="exception">The exception which properties will be added to the bag.</param>
/// <param name="filter">Filter that should be applied to each property just before adding it to the bag.</param>
public ExceptionPropertiesBag(Exception exception, IExceptionPropertyFilter filter = null)
public ExceptionPropertiesBag(Exception exception, IExceptionPropertyFilter? filter = null)
{
this.exception = exception ?? throw new ArgumentNullException(nameof(exception));
this.filter = filter;
}

/// <inheritdoc />
public IReadOnlyDictionary<string, object> GetResultDictionary()
public IReadOnlyDictionary<string, object?> GetResultDictionary()
{
this.resultsCollected = true;
return this.properties;
}

/// <inheritdoc />
public void AddProperty(string key, object value)
public void AddProperty(string key, object? value)
{
if (key is null)
{
Expand Down
82 changes: 41 additions & 41 deletions Source/Serilog.Exceptions/Core/IDestructuringOptions.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
namespace Serilog.Exceptions.Core
{
using System.Collections.Generic;
using Serilog.Exceptions.Destructurers;
using Serilog.Exceptions.Filters;

/// <summary>
/// Represents all the configuration options user can specify to influence the destructuring process.
/// </summary>
public interface IDestructuringOptions
{
/// <summary>
namespace Serilog.Exceptions.Core
{
using System.Collections.Generic;
using Serilog.Exceptions.Destructurers;
using Serilog.Exceptions.Filters;

/// <summary>
/// Represents all the configuration options user can specify to influence the destructuring process.
/// </summary>
public interface IDestructuringOptions
{
/// <summary>
/// Gets the name of the key dictionary to which destructured exception will be assigned. Default is
/// <c>"ExceptionDetail"</c>.
/// </summary>
string RootName { get; }

/// <summary>
/// Gets the depth at which reflection based destructurer will stop recursive process of children destructuring.
/// Default is <c>10</c>.
/// </summary>
int DestructuringDepth { get; }

/// <summary>
/// Gets the collection of destructurers that will be used to destructure incoming exceptions. If none of the
/// destructurers can handle given type of exception, a generic, reflection-based destructurer will be used.
/// </summary>
IEnumerable<IExceptionDestructurer> Destructurers { get; }

/// <summary>
/// Gets the optional filter that will evaluate and possibly reject each destructured property just before
/// they are about to be written to a result structure. If no filter is set no properties are going to be
/// rejected. Filter is applied to every property regardless of which destructurer was used.
/// </summary>
IExceptionPropertyFilter Filter { get; }

/// <c>"ExceptionDetail"</c>.
/// </summary>
string RootName { get; }

/// <summary>
/// Gets the depth at which reflection based destructurer will stop recursive process of children destructuring.
/// Default is <c>10</c>.
/// </summary>
int DestructuringDepth { get; }

/// <summary>
/// Gets the collection of destructurers that will be used to destructure incoming exceptions. If none of the
/// destructurers can handle given type of exception, a generic, reflection-based destructurer will be used.
/// </summary>
IEnumerable<IExceptionDestructurer> Destructurers { get; }

/// <summary>
/// Gets the optional filter that will evaluate and possibly reject each destructured property just before
/// they are about to be written to a result structure. If no filter is set no properties are going to be
/// rejected. Filter is applied to every property regardless of which destructurer was used.
/// </summary>
IExceptionPropertyFilter? Filter { get; }

/// <summary>
/// Gets a value indicating whether to disable the reflection based destructurer.
/// You may want to disable this destructurer if you need full control
/// Gets a value indicating whether to disable the reflection based destructurer.
/// You may want to disable this destructurer if you need full control
/// over the process of destructuring and want to provide all the destructurers yourself.
/// </summary>
bool DisableReflectionBasedDestructurer { get; }
}
}
/// </summary>
bool DisableReflectionBasedDestructurer { get; }
}
}
4 changes: 2 additions & 2 deletions Source/Serilog.Exceptions/Core/IExceptionPropertiesBag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ public interface IExceptionPropertiesBag
/// method.
/// </summary>
/// <returns>Dictionary with all the properties names and values that were added.</returns>
IReadOnlyDictionary<string, object> GetResultDictionary();
IReadOnlyDictionary<string, object?> GetResultDictionary();

/// <summary>
/// Adds a property to the bag.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
void AddProperty(string key, object value);
void AddProperty(string key, object? value);

/// <summary>
/// Returns <c>true</c> if given key is already present in the bag.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class AggregateExceptionDestructurer : ExceptionDestructurer
public override void Destructure(
Exception exception,
IExceptionPropertiesBag propertiesBag,
Func<Exception, IReadOnlyDictionary<string, object>> destructureException)
Func<Exception, IReadOnlyDictionary<string, object?>?> destructureException)
{
base.Destructure(exception, propertiesBag, destructureException);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ArgumentExceptionDestructurer : ExceptionDestructurer
public override void Destructure(
Exception exception,
IExceptionPropertiesBag propertiesBag,
Func<Exception, IReadOnlyDictionary<string, object>> destructureException)
Func<Exception, IReadOnlyDictionary<string, object?>?> destructureException)
{
base.Destructure(exception, propertiesBag, destructureException);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ArgumentOutOfRangeExceptionDestructurer : ArgumentExceptionDestruct
public override void Destructure(
Exception exception,
IExceptionPropertiesBag propertiesBag,
Func<Exception, IReadOnlyDictionary<string, object>> destructureException)
Func<Exception, IReadOnlyDictionary<string, object?>?> destructureException)
{
base.Destructure(exception, propertiesBag, destructureException);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public virtual Type[] TargetTypes
{
var targetTypes = new List<Type>
{
#pragma warning disable IDE0001 // Simplify Names
#if NET461 || NET472
typeof(Microsoft.SqlServer.Server.InvalidUdtException),
typeof(System.AccessViolationException),
Expand Down Expand Up @@ -174,6 +175,7 @@ public virtual Type[] TargetTypes
typeof(System.UnauthorizedAccessException),
typeof(System.UriFormatException),
};
#pragma warning restore IDE0001 // Simplify Names

#if NET461 || NET472
foreach (var dangerousType in GetNotHandledByMonoTypes())
Expand All @@ -193,7 +195,7 @@ public virtual Type[] TargetTypes
public virtual void Destructure(
Exception exception,
IExceptionPropertiesBag propertiesBag,
Func<Exception, IReadOnlyDictionary<string, object>> destructureException)
Func<Exception, IReadOnlyDictionary<string, object?>?> destructureException)
{
if (exception is null)
{
Expand Down Expand Up @@ -230,7 +232,7 @@ public virtual void Destructure(
internal static void DestructureCommonExceptionProperties(
Exception exception,
IExceptionPropertiesBag propertiesBag,
Func<Exception, IReadOnlyDictionary<string, object>> innerDestructure,
Func<Exception, IReadOnlyDictionary<string, object?>?> innerDestructure,
Func<System.Collections.IDictionary, object> destructureDataProperty)
{
if (exception.Data.Count != 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public interface IExceptionDestructurer
void Destructure(
Exception exception,
IExceptionPropertiesBag propertiesBag,
Func<Exception, IReadOnlyDictionary<string, object>> destructureException);
Func<Exception, IReadOnlyDictionary<string, object?>?> destructureException);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class OperationCanceledExceptionDestructurer : ExceptionDestructurer
public override void Destructure(
Exception exception,
IExceptionPropertiesBag propertiesBag,
Func<Exception, IReadOnlyDictionary<string, object>> destructureException)
Func<Exception, IReadOnlyDictionary<string, object?>?> destructureException)
{
base.Destructure(exception, propertiesBag, destructureException);

Expand Down
Loading

0 comments on commit 2945cee

Please sign in to comment.