Skip to content

Commit

Permalink
Implement IAsyncDisposable on remaining sink wrapper types
Browse files Browse the repository at this point in the history
  • Loading branch information
nblumhardt committed Sep 11, 2022
1 parent 0177758 commit 13fcaf0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
16 changes: 15 additions & 1 deletion src/Serilog/Core/Sinks/ConditionalSink.cs
Expand Up @@ -14,7 +14,10 @@

namespace Serilog.Core.Sinks;

class ConditionalSink : ILogEventSink, IDisposable
sealed class ConditionalSink : ILogEventSink, IDisposable
#if FEATURE_ASYNCDISPOSABLE
, IAsyncDisposable
#endif
{
readonly ILogEventSink _wrapped;
readonly Func<LogEvent, bool> _condition;
Expand All @@ -35,4 +38,15 @@ public void Dispose()
{
(_wrapped as IDisposable)?.Dispose();
}

#if FEATURE_ASYNCDISPOSABLE
public ValueTask DisposeAsync()
{
if (_wrapped is IAsyncDisposable asyncDisposable)
return asyncDisposable.DisposeAsync();

Dispose();
return default;
}
#endif
}
16 changes: 15 additions & 1 deletion src/Serilog/Core/Sinks/RestrictedSink.cs
Expand Up @@ -14,7 +14,10 @@

namespace Serilog.Core.Sinks;

class RestrictedSink : ILogEventSink, IDisposable
sealed class RestrictedSink : ILogEventSink, IDisposable
#if FEATURE_ASYNCDISPOSABLE
, IAsyncDisposable
#endif
{
readonly ILogEventSink _sink;
readonly LoggingLevelSwitch _levelSwitch;
Expand All @@ -39,4 +42,15 @@ public void Dispose()
{
(_sink as IDisposable)?.Dispose();
}

#if FEATURE_ASYNCDISPOSABLE
public ValueTask DisposeAsync()
{
if (_sink is IAsyncDisposable asyncDisposable)
return asyncDisposable.DisposeAsync();

Dispose();
return default;
}
#endif
}
21 changes: 20 additions & 1 deletion src/Serilog/Core/Sinks/SecondaryLoggerSink.cs
Expand Up @@ -21,7 +21,10 @@ namespace Serilog.Core.Sinks;
/// <remarks>The properties dictionary is copied, however the values within
/// the dictionary (of type <see cref="LogEventProperty"/> are expected to
/// be immutable.</remarks>
class SecondaryLoggerSink : ILogEventSink, IDisposable
sealed class SecondaryLoggerSink : ILogEventSink, IDisposable
#if FEATURE_ASYNCDISPOSABLE
, IAsyncDisposable
#endif
{
readonly ILogger _logger;
readonly bool _attemptDispose;
Expand All @@ -47,4 +50,20 @@ public void Dispose()

(_logger as IDisposable)?.Dispose();
}

#if FEATURE_ASYNCDISPOSABLE
public ValueTask DisposeAsync()
{
if (_logger is IAsyncDisposable asyncDisposable)
{
if (!_attemptDispose)
return default;

return asyncDisposable.DisposeAsync();
}

Dispose();
return default;
}
#endif
}

0 comments on commit 13fcaf0

Please sign in to comment.