Skip to content

Commit

Permalink
guard against null (#1742)
Browse files Browse the repository at this point in the history
* guard against null

* Create ArgumentNullTests.cs

* Update Guard.cs
  • Loading branch information
SimonCropp committed Sep 9, 2022
1 parent fd36c3e commit 4d13be5
Show file tree
Hide file tree
Showing 57 changed files with 235 additions and 151 deletions.
4 changes: 2 additions & 2 deletions src/Serilog/Capturing/PropertyValueConverter.cs
Expand Up @@ -49,8 +49,8 @@ partial class PropertyValueConverter : ILogEventPropertyFactory, ILogEventProper
IEnumerable<IDestructuringPolicy> additionalDestructuringPolicies,
bool propagateExceptions)
{
if (additionalScalarTypes == null) throw new ArgumentNullException(nameof(additionalScalarTypes));
if (additionalDestructuringPolicies == null) throw new ArgumentNullException(nameof(additionalDestructuringPolicies));
Guard.AgainstNull(additionalScalarTypes);
Guard.AgainstNull(additionalDestructuringPolicies);
if (maximumDestructuringDepth < 0) throw new ArgumentOutOfRangeException(nameof(maximumDestructuringDepth));
if (maximumStringLength < 2) throw new ArgumentOutOfRangeException(nameof(maximumStringLength));
if (maximumCollectionCount < 1) throw new ArgumentOutOfRangeException(nameof(maximumCollectionCount));
Expand Down
22 changes: 11 additions & 11 deletions src/Serilog/Configuration/LoggerDestructuringConfiguration.cs
Expand Up @@ -34,12 +34,12 @@ public class LoggerDestructuringConfiguration
Action<int> setMaximumStringLength,
Action<int> setMaximumCollectionCount)
{
_loggerConfiguration = loggerConfiguration ?? throw new ArgumentNullException(nameof(loggerConfiguration));
_addScalar = addScalar ?? throw new ArgumentNullException(nameof(addScalar));
_addPolicy = addPolicy ?? throw new ArgumentNullException(nameof(addPolicy));
_setMaximumDepth = setMaximumDepth ?? throw new ArgumentNullException(nameof(setMaximumDepth));
_setMaximumStringLength = setMaximumStringLength ?? throw new ArgumentNullException(nameof(setMaximumStringLength));
_setMaximumCollectionCount = setMaximumCollectionCount ?? throw new ArgumentNullException(nameof(setMaximumCollectionCount));
_loggerConfiguration = Guard.AgainstNull(loggerConfiguration);
_addScalar = Guard.AgainstNull(addScalar);
_addPolicy = Guard.AgainstNull(addPolicy);
_setMaximumDepth = Guard.AgainstNull(setMaximumDepth);
_setMaximumStringLength = Guard.AgainstNull(setMaximumStringLength);
_setMaximumCollectionCount = Guard.AgainstNull(setMaximumCollectionCount);
}

/// <summary>
Expand All @@ -51,7 +51,7 @@ public class LoggerDestructuringConfiguration
/// <exception cref="ArgumentNullException">When <paramref name="scalarType"/> is <code>null</code></exception>
public LoggerConfiguration AsScalar(Type scalarType)
{
if (scalarType == null) throw new ArgumentNullException(nameof(scalarType));
Guard.AgainstNull(scalarType);

_addScalar(scalarType);
return _loggerConfiguration;
Expand All @@ -75,7 +75,7 @@ public LoggerConfiguration AsScalar(Type scalarType)
// ReSharper disable once MemberCanBePrivate.Global
public LoggerConfiguration With(params IDestructuringPolicy[] destructuringPolicies)
{
if (destructuringPolicies == null) throw new ArgumentNullException(nameof(destructuringPolicies));
Guard.AgainstNull(destructuringPolicies);

foreach (var destructuringPolicy in destructuringPolicies)
{
Expand Down Expand Up @@ -108,7 +108,7 @@ public LoggerConfiguration With<TDestructuringPolicy>()
/// <exception cref="ArgumentNullException">When <paramref name="transformation"/> is <code>null</code></exception>
public LoggerConfiguration ByTransforming<TValue>(Func<TValue, object> transformation)
{
if (transformation == null) throw new ArgumentNullException(nameof(transformation));
Guard.AgainstNull(transformation);

var policy = new ProjectedDestructuringPolicy(t => t == typeof(TValue),
o => transformation((TValue)o));
Expand All @@ -132,8 +132,8 @@ public LoggerConfiguration ByTransforming<TValue>(Func<TValue, object> transform
Func<Type, bool> predicate,
Func<TValue, object> transformation)
{
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
if (transformation == null) throw new ArgumentNullException(nameof(transformation));
Guard.AgainstNull(predicate);
Guard.AgainstNull(transformation);

var policy = new ProjectedDestructuringPolicy(predicate,
o => transformation((TValue)o));
Expand Down
22 changes: 11 additions & 11 deletions src/Serilog/Configuration/LoggerEnrichmentConfiguration.cs
Expand Up @@ -26,8 +26,8 @@ public class LoggerEnrichmentConfiguration
LoggerConfiguration loggerConfiguration,
Action<ILogEventEnricher> addEnricher)
{
_loggerConfiguration = loggerConfiguration ?? throw new ArgumentNullException(nameof(loggerConfiguration));
_addEnricher = addEnricher ?? throw new ArgumentNullException(nameof(addEnricher));
_loggerConfiguration = Guard.AgainstNull(loggerConfiguration);
_addEnricher = Guard.AgainstNull(addEnricher);
}

/// <summary>
Expand All @@ -41,11 +41,11 @@ public class LoggerEnrichmentConfiguration
/// <exception cref="ArgumentException">When any element of <paramref name="enrichers"/> is <code>null</code></exception>
public LoggerConfiguration With(params ILogEventEnricher[] enrichers)
{
if (enrichers == null) throw new ArgumentNullException(nameof(enrichers));
Guard.AgainstNull(enrichers);

foreach (var logEventEnricher in enrichers)
{
if (logEventEnricher == null) throw new ArgumentException("Null enricher is not allowed.");
Guard.AgainstNull(logEventEnricher);

_addEnricher(logEventEnricher);
}
Expand Down Expand Up @@ -96,8 +96,8 @@ public LoggerConfiguration WithProperty(string name, object value, bool destruct
/// <exception cref="ArgumentNullException">When <paramref name="configureEnricher"/> is <code>null</code></exception>
public LoggerConfiguration When(Func<LogEvent, bool> condition, Action<LoggerEnrichmentConfiguration> configureEnricher)
{
if (condition == null) throw new ArgumentNullException(nameof(condition));
if (configureEnricher == null) throw new ArgumentNullException(nameof(configureEnricher));
Guard.AgainstNull(condition);
Guard.AgainstNull(configureEnricher);

return Wrap(this, e => new ConditionalEnricher(e, condition), configureEnricher);
}
Expand All @@ -113,7 +113,7 @@ public LoggerConfiguration When(Func<LogEvent, bool> condition, Action<LoggerEnr
/// <exception cref="ArgumentNullException">When <paramref name="configureEnricher"/> is <code>null</code></exception>
public LoggerConfiguration AtLevel(LogEventLevel enrichFromLevel, Action<LoggerEnrichmentConfiguration> configureEnricher)
{
if (configureEnricher == null) throw new ArgumentNullException(nameof(configureEnricher));
Guard.AgainstNull(configureEnricher);

return Wrap(this, e => new ConditionalEnricher(e, le => le.Level >= enrichFromLevel), configureEnricher);
}
Expand All @@ -129,7 +129,7 @@ public LoggerConfiguration AtLevel(LogEventLevel enrichFromLevel, Action<LoggerE
/// <exception cref="ArgumentNullException">When <paramref name="configureEnricher"/> is <code>null</code></exception>
public LoggerConfiguration AtLevel(LoggingLevelSwitch levelSwitch, Action<LoggerEnrichmentConfiguration> configureEnricher)
{
if (configureEnricher == null) throw new ArgumentNullException(nameof(configureEnricher));
Guard.AgainstNull(configureEnricher);

return Wrap(this, e => new ConditionalEnricher(e, le => le.Level >= levelSwitch.MinimumLevel), configureEnricher);
}
Expand All @@ -150,9 +150,9 @@ public LoggerConfiguration AtLevel(LoggingLevelSwitch levelSwitch, Action<Logger
Func<ILogEventEnricher, ILogEventEnricher> wrapEnricher,
Action<LoggerEnrichmentConfiguration> configureWrappedEnricher)
{
if (loggerEnrichmentConfiguration == null) throw new ArgumentNullException(nameof(loggerEnrichmentConfiguration));
if (wrapEnricher == null) throw new ArgumentNullException(nameof(wrapEnricher));
if (configureWrappedEnricher == null) throw new ArgumentNullException(nameof(configureWrappedEnricher));
Guard.AgainstNull(loggerEnrichmentConfiguration);
Guard.AgainstNull(wrapEnricher);
Guard.AgainstNull(configureWrappedEnricher);

var enrichersToWrap = new List<ILogEventEnricher>();

Expand Down
6 changes: 3 additions & 3 deletions src/Serilog/Configuration/LoggerFilterConfiguration.cs
Expand Up @@ -26,8 +26,8 @@ public class LoggerFilterConfiguration
LoggerConfiguration loggerConfiguration,
Action<ILogEventFilter> addFilter)
{
_loggerConfiguration = loggerConfiguration ?? throw new ArgumentNullException(nameof(loggerConfiguration));
_addFilter = addFilter ?? throw new ArgumentNullException(nameof(addFilter));
_loggerConfiguration = Guard.AgainstNull(loggerConfiguration);
_addFilter = Guard.AgainstNull(addFilter);
}

/// <summary>
Expand All @@ -39,7 +39,7 @@ public class LoggerFilterConfiguration
/// <exception cref="ArgumentException">When any element of <paramref name="filters"/> is <code>null</code></exception>
public LoggerConfiguration With(params ILogEventFilter[] filters)
{
if (filters == null) throw new ArgumentNullException(nameof(filters));
Guard.AgainstNull(filters);

foreach (var logEventFilter in filters)
{
Expand Down
12 changes: 6 additions & 6 deletions src/Serilog/Configuration/LoggerMinimumLevelConfiguration.cs
Expand Up @@ -27,10 +27,10 @@ public class LoggerMinimumLevelConfiguration
internal LoggerMinimumLevelConfiguration(LoggerConfiguration loggerConfiguration, Action<LogEventLevel> setMinimum,
Action<LoggingLevelSwitch> setLevelSwitch, Action<string, LoggingLevelSwitch> addOverride)
{
_loggerConfiguration = loggerConfiguration ?? throw new ArgumentNullException(nameof(loggerConfiguration));
_setMinimum = setMinimum ?? throw new ArgumentNullException(nameof(setMinimum));
_loggerConfiguration = Guard.AgainstNull(loggerConfiguration);
_setMinimum = Guard.AgainstNull(setMinimum);
_setLevelSwitch = setLevelSwitch;
_addOverride = addOverride ?? throw new ArgumentNullException(nameof(addOverride));
_addOverride = Guard.AgainstNull(addOverride);
}

/// <summary>
Expand All @@ -53,7 +53,7 @@ public LoggerConfiguration Is(LogEventLevel minimumLevel)
// ReSharper disable once UnusedMethodReturnValue.Global
public LoggerConfiguration ControlledBy(LoggingLevelSwitch levelSwitch)
{
if (levelSwitch == null) throw new ArgumentNullException(nameof(levelSwitch));
Guard.AgainstNull(levelSwitch);

_setLevelSwitch(levelSwitch);
return _loggerConfiguration;
Expand Down Expand Up @@ -113,8 +113,8 @@ public LoggerConfiguration ControlledBy(LoggingLevelSwitch levelSwitch)
/// <exception cref="ArgumentNullException">When <paramref name="levelSwitch"/> is <code>null</code></exception>
public LoggerConfiguration Override(string source, LoggingLevelSwitch levelSwitch)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (levelSwitch == null) throw new ArgumentNullException(nameof(levelSwitch));
Guard.AgainstNull(source);
Guard.AgainstNull(levelSwitch);

var trimmed = source.Trim();
if (trimmed.Length == 0) throw new ArgumentException($"A source {nameof(source)} must be provided.", nameof(source));
Expand Down
6 changes: 3 additions & 3 deletions src/Serilog/Configuration/LoggerSettingsConfiguration.cs
Expand Up @@ -23,7 +23,7 @@ public class LoggerSettingsConfiguration

internal LoggerSettingsConfiguration(LoggerConfiguration loggerConfiguration)
{
_loggerConfiguration = loggerConfiguration ?? throw new ArgumentNullException(nameof(loggerConfiguration));
_loggerConfiguration = Guard.AgainstNull(loggerConfiguration);
}

/// <summary>
Expand All @@ -33,7 +33,7 @@ internal LoggerSettingsConfiguration(LoggerConfiguration loggerConfiguration)
/// <exception cref="ArgumentNullException">When <paramref name="settings"/> is <code>null</code></exception>
public LoggerConfiguration Settings(ILoggerSettings settings)
{
if (settings == null) throw new ArgumentNullException(nameof(settings));
Guard.AgainstNull(settings);

settings.Configure(_loggerConfiguration);
return _loggerConfiguration;
Expand All @@ -48,7 +48,7 @@ public LoggerConfiguration Settings(ILoggerSettings settings)
/// <exception cref="ArgumentNullException">When <paramref name="settings"/> is <code>null</code></exception>
public LoggerConfiguration KeyValuePairs(IEnumerable<KeyValuePair<string, string>> settings)
{
if (settings == null) throw new ArgumentNullException(nameof(settings));
Guard.AgainstNull(settings);

var uniqueSettings = new Dictionary<string, string>();
foreach (var kvp in settings)
Expand Down
18 changes: 9 additions & 9 deletions src/Serilog/Configuration/LoggerSinkConfiguration.cs
Expand Up @@ -24,8 +24,8 @@ public class LoggerSinkConfiguration

internal LoggerSinkConfiguration(LoggerConfiguration loggerConfiguration, Action<ILogEventSink> addSink)
{
_loggerConfiguration = loggerConfiguration ?? throw new ArgumentNullException(nameof(loggerConfiguration));
_addSink = addSink ?? throw new ArgumentNullException(nameof(addSink));
_loggerConfiguration = Guard.AgainstNull(loggerConfiguration);
_addSink = Guard.AgainstNull(addSink);
}

/// <summary>
Expand Down Expand Up @@ -113,7 +113,7 @@ internal LoggerSinkConfiguration(LoggerConfiguration loggerConfiguration, Action
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
LoggingLevelSwitch? levelSwitch = null)
{
if (configureLogger == null) throw new ArgumentNullException(nameof(configureLogger));
Guard.AgainstNull(configureLogger);

var lc = new LoggerConfiguration().MinimumLevel.Is(LevelAlias.Minimum);
configureLogger(lc);
Expand Down Expand Up @@ -145,7 +145,7 @@ internal LoggerSinkConfiguration(LoggerConfiguration loggerConfiguration, Action
ILogger logger,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum)
{
if (logger == null) throw new ArgumentNullException(nameof(logger));
Guard.AgainstNull(logger);

if (logger is Logger {HasOverrideMap: true})
{
Expand All @@ -168,8 +168,8 @@ internal LoggerSinkConfiguration(LoggerConfiguration loggerConfiguration, Action
/// <exception cref="ArgumentNullException">When <paramref name="configureSink"/> is <code>null</code></exception>
public LoggerConfiguration Conditional(Func<LogEvent, bool> condition, Action<LoggerSinkConfiguration> configureSink)
{
if (condition == null) throw new ArgumentNullException(nameof(condition));
if (configureSink == null) throw new ArgumentNullException(nameof(configureSink));
Guard.AgainstNull(condition);
Guard.AgainstNull(configureSink);

// Level aliases and so on don't need to be accepted here; if the user wants both a condition and leveling, they
// can specify `restrictedToMinimumLevel` etc in the wrapped sink configuration.
Expand Down Expand Up @@ -215,9 +215,9 @@ public LoggerConfiguration Conditional(Func<LogEvent, bool> condition, Action<Lo
LogEventLevel restrictedToMinimumLevel,
LoggingLevelSwitch? levelSwitch)
{
if (loggerSinkConfiguration == null) throw new ArgumentNullException(nameof(loggerSinkConfiguration));
if (wrapSink == null) throw new ArgumentNullException(nameof(wrapSink));
if (configureWrappedSink == null) throw new ArgumentNullException(nameof(configureWrappedSink));
Guard.AgainstNull(loggerSinkConfiguration);
Guard.AgainstNull(wrapSink);
Guard.AgainstNull(configureWrappedSink);

var sinksToWrap = new List<ILogEventSink>();

Expand Down
2 changes: 1 addition & 1 deletion src/Serilog/Context/EnricherStack.cs
Expand Up @@ -31,7 +31,7 @@ class EnricherStack: IEnumerable<ILogEventEnricher>

EnricherStack(EnricherStack under, ILogEventEnricher top)
{
_under = under ?? throw new ArgumentNullException(nameof(under));
_under = Guard.AgainstNull(under);
Count = under.Count + 1;
_top = top;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Serilog/Context/LogContext.cs
Expand Up @@ -75,7 +75,7 @@ public static IDisposable PushProperty(string name, object value, bool destructu
/// <exception cref="ArgumentNullException">When <paramref name="enricher"/> is <code>null</code></exception>
public static IDisposable Push(ILogEventEnricher enricher)
{
if (enricher == null) throw new ArgumentNullException(nameof(enricher));
Guard.AgainstNull(enricher);

var stack = GetOrCreateEnricherStack();
var bookmark = new ContextStackBookmark(stack);
Expand All @@ -97,7 +97,7 @@ public static IDisposable Push(ILogEventEnricher enricher)
/// <exception cref="ArgumentNullException">When <paramref name="enrichers"/> is <code>null</code></exception>
public static IDisposable Push(params ILogEventEnricher[] enrichers)
{
if (enrichers == null) throw new ArgumentNullException(nameof(enrichers));
Guard.AgainstNull(enrichers);

var stack = GetOrCreateEnricherStack();
var bookmark = new ContextStackBookmark(stack);
Expand Down
4 changes: 2 additions & 2 deletions src/Serilog/Core/Enrichers/ConditionalEnricher.cs
Expand Up @@ -21,8 +21,8 @@ class ConditionalEnricher : ILogEventEnricher, IDisposable

public ConditionalEnricher(ILogEventEnricher wrapped, Func<LogEvent, bool> condition)
{
_wrapped = wrapped ?? throw new ArgumentNullException(nameof(wrapped));
_condition = condition ?? throw new ArgumentNullException(nameof(condition));
_wrapped = Guard.AgainstNull(wrapped);
_condition = Guard.AgainstNull(condition);
}

public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
Expand Down
2 changes: 1 addition & 1 deletion src/Serilog/Core/Enrichers/FixedPropertyEnricher.cs
Expand Up @@ -27,7 +27,7 @@ public FixedPropertyEnricher(in EventProperty eventProperty)

public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
if (logEvent == null) throw new ArgumentNullException(nameof(logEvent));
Guard.AgainstNull(logEvent);

logEvent.AddPropertyIfAbsent(_eventProperty);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Serilog/Core/Enrichers/PropertyEnricher.cs
Expand Up @@ -51,8 +51,8 @@ public PropertyEnricher(string name, object? value, bool destructureObjects = fa
/// <exception cref="ArgumentNullException">When <paramref name="propertyFactory"/> is <code>null</code></exception>
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
if (logEvent == null) throw new ArgumentNullException(nameof(logEvent));
if (propertyFactory == null) throw new ArgumentNullException(nameof(propertyFactory));
Guard.AgainstNull(logEvent);
Guard.AgainstNull(propertyFactory);

var property = propertyFactory.CreateProperty(_name, _value, _destructureObjects);
logEvent.AddPropertyIfAbsent(property);
Expand Down
2 changes: 1 addition & 1 deletion src/Serilog/Core/Enrichers/SafeAggregateEnricher.cs
Expand Up @@ -20,7 +20,7 @@ class SafeAggregateEnricher : ILogEventEnricher

public SafeAggregateEnricher(IEnumerable<ILogEventEnricher> enrichers)
{
if (enrichers == null) throw new ArgumentNullException(nameof(enrichers));
Guard.AgainstNull(enrichers);

_enrichers = enrichers.ToArray();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Serilog/Core/Filters/DelegateFilter.cs
Expand Up @@ -20,12 +20,12 @@ class DelegateFilter : ILogEventFilter

public DelegateFilter(Func<LogEvent, bool> isEnabled)
{
_isEnabled = isEnabled ?? throw new ArgumentNullException(nameof(isEnabled));
_isEnabled = Guard.AgainstNull(isEnabled);
}

public bool IsEnabled(LogEvent logEvent)
{
if (logEvent == null) throw new ArgumentNullException(nameof(logEvent));
Guard.AgainstNull(logEvent);
return _isEnabled(logEvent);
}
}
2 changes: 1 addition & 1 deletion src/Serilog/Core/LevelOverrideMap.cs
Expand Up @@ -45,7 +45,7 @@ public LevelOverride(string context, LoggingLevelSwitch levelSwitch)
LogEventLevel defaultMinimumLevel,
LoggingLevelSwitch? defaultLevelSwitch)
{
if (overrides == null) throw new ArgumentNullException(nameof(overrides));
Guard.AgainstNull(overrides);

_defaultLevelSwitch = defaultLevelSwitch;
_defaultMinimumLevel = defaultLevelSwitch != null ? LevelAlias.Minimum : defaultMinimumLevel;
Expand Down
2 changes: 1 addition & 1 deletion src/Serilog/Core/Logger.cs
Expand Up @@ -379,7 +379,7 @@ public void Write(LogEvent logEvent)

void ILogEventSink.Emit(LogEvent logEvent)
{
if (logEvent == null) throw new ArgumentNullException(nameof(logEvent));
Guard.AgainstNull(logEvent);

// Bypasses the level check so that child loggers
// using this one as a sink can increase verbosity.
Expand Down

0 comments on commit 4d13be5

Please sign in to comment.