From 522e7bc69ac68ffe766ca00d64f8dfa601228b46 Mon Sep 17 00:00:00 2001 From: Wolf Bublitz Date: Wed, 15 Apr 2026 09:15:43 +0200 Subject: [PATCH] feat: enforce non-nullable payloads in logging interfaces --- src/ILogMessage.cs | 3 ++- src/ILogger.cs | 3 ++- src/ILoggerExtensions.cs | 3 +++ src/LogSinks/IAsyncLogSink.cs | 3 ++- src/LogSinks/ILogSink.cs | 3 ++- src/LogSinks/ILogSink{TPayload}.cs | 1 + 6 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/ILogMessage.cs b/src/ILogMessage.cs index aff45ce..046e6cf 100644 --- a/src/ILogMessage.cs +++ b/src/ILogMessage.cs @@ -8,6 +8,7 @@ namespace WB.Logging; /// A log message. /// public interface ILogMessage + where TPayload : notnull { // ┌─────────────────────────────────────────────────────────────────────────────┐ // │ Public Properties │ @@ -31,5 +32,5 @@ public interface ILogMessage /// /// Gets the payload of the log message. /// - public TPayload? Payload { get; } + public TPayload Payload { get; } } \ No newline at end of file diff --git a/src/ILogger.cs b/src/ILogger.cs index 2b12f2f..983e7eb 100644 --- a/src/ILogger.cs +++ b/src/ILogger.cs @@ -51,7 +51,8 @@ public interface ILogger : IAsyncDisposable /// /// The . /// The payload to log. - public void Log(LogLevel? logLevel, TPayload payload); + public void Log(LogLevel? logLevel, TPayload payload) + where TPayload : notnull; /// /// Flushes all pending log messages. diff --git a/src/ILoggerExtensions.cs b/src/ILoggerExtensions.cs index 0480e24..c32d0c1 100644 --- a/src/ILoggerExtensions.cs +++ b/src/ILoggerExtensions.cs @@ -30,6 +30,7 @@ public void Exception(Exception exception) /// The payload to log. [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Info(TPayload payload) + where TPayload : notnull => @this.Log(LogLevel.Info, payload); /// @@ -38,6 +39,7 @@ public void Info(TPayload payload) /// The payload to log. [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Warning(TPayload payload) + where TPayload : notnull => @this.Log(LogLevel.Warning, payload); /// @@ -47,6 +49,7 @@ public void Warning(TPayload payload) [System.Diagnostics.CodeAnalysis.SuppressMessage("Naming", "CA1716:Identifiers should not match keywords", Justification = "Common logging term.")] [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Error(TPayload payload) + where TPayload : notnull => @this.Log(LogLevel.Error, payload); } } \ No newline at end of file diff --git a/src/LogSinks/IAsyncLogSink.cs b/src/LogSinks/IAsyncLogSink.cs index ada5568..827352e 100644 --- a/src/LogSinks/IAsyncLogSink.cs +++ b/src/LogSinks/IAsyncLogSink.cs @@ -17,5 +17,6 @@ public interface IAsyncLogSink /// The to submit. /// The type of the payload of the log message. /// A that represents the asynchronous operation of submitting the log message. - public ValueTask SubmitAsync(ILogMessage logMessage); + public ValueTask SubmitAsync(ILogMessage logMessage) + where TPayload : notnull; } \ No newline at end of file diff --git a/src/LogSinks/ILogSink.cs b/src/LogSinks/ILogSink.cs index 2d7b306..07198a7 100644 --- a/src/LogSinks/ILogSink.cs +++ b/src/LogSinks/ILogSink.cs @@ -14,5 +14,6 @@ public interface ILogSink /// /// The to submit. /// The type of the payload of the log message. - public void Submit(ILogMessage logMessage); + public void Submit(ILogMessage logMessage) + where TPayload : notnull; } \ No newline at end of file diff --git a/src/LogSinks/ILogSink{TPayload}.cs b/src/LogSinks/ILogSink{TPayload}.cs index 1e56d2c..b61900e 100644 --- a/src/LogSinks/ILogSink{TPayload}.cs +++ b/src/LogSinks/ILogSink{TPayload}.cs @@ -6,6 +6,7 @@ namespace WB.Logging; /// /// The type of the payload of the log messages that this log sink can process. public interface ILogSink + where TPayload : notnull { // ┌─────────────────────────────────────────────────────────────────────────────┐ // │ Public Methods │