From 2359cef64f51f9792e66240e8f3df6f66c268f87 Mon Sep 17 00:00:00 2001 From: Jaben Cargman Date: Fri, 20 Sep 2024 01:04:50 -0400 Subject: [PATCH] Nullability fixes. --- src/SerilogCommonLogger.cs | 52 ++++++++--------- src/SerilogInstanceWrapper.cs | 102 +++++++++++++++++----------------- 2 files changed, 76 insertions(+), 78 deletions(-) diff --git a/src/SerilogCommonLogger.cs b/src/SerilogCommonLogger.cs index 8cfc4dc..defc098 100644 --- a/src/SerilogCommonLogger.cs +++ b/src/SerilogCommonLogger.cs @@ -59,7 +59,7 @@ public virtual void Trace(object message, Exception exception) Write(LogLevel.Trace, exception, message); } - public virtual void TraceFormat(IFormatProvider formatProvider, string format, params object[] args) + public virtual void TraceFormat(IFormatProvider? formatProvider, string format, params object[] args) { if (!IsTraceEnabled) return; @@ -67,7 +67,7 @@ public virtual void TraceFormat(IFormatProvider formatProvider, string format, p WriteFormat(LogLevel.Trace, formatProvider, format, args); } - public virtual void TraceFormat(IFormatProvider formatProvider, string format, Exception exception, params object[] args) + public virtual void TraceFormat(IFormatProvider? formatProvider, string format, Exception? exception, params object[] args) { if (!IsTraceEnabled) return; @@ -83,7 +83,7 @@ public virtual void TraceFormat(string format, params object[] args) WriteFormat(LogLevel.Trace, format, args); } - public virtual void TraceFormat(string format, Exception exception, params object[] args) + public virtual void TraceFormat(string format, Exception? exception, params object[] args) { if (!IsTraceEnabled) return; @@ -139,7 +139,7 @@ public virtual void Debug(object message, Exception exception) Write(LogLevel.Debug, exception, message); } - public virtual void DebugFormat(IFormatProvider formatProvider, string format, params object[] args) + public virtual void DebugFormat(IFormatProvider? formatProvider, string format, params object[] args) { if (!IsDebugEnabled) return; @@ -147,7 +147,7 @@ public virtual void DebugFormat(IFormatProvider formatProvider, string format, p WriteFormat(LogLevel.Debug, formatProvider, format, args); } - public virtual void DebugFormat(IFormatProvider formatProvider, string format, Exception exception, params object[] args) + public virtual void DebugFormat(IFormatProvider? formatProvider, string format, Exception? exception, params object[] args) { if (!IsDebugEnabled) return; @@ -163,7 +163,7 @@ public virtual void DebugFormat(string format, params object[] args) WriteFormat(LogLevel.Debug, format, args); } - public virtual void DebugFormat(string format, Exception exception, params object[] args) + public virtual void DebugFormat(string format, Exception? exception, params object[] args) { if (!IsDebugEnabled) return; @@ -219,7 +219,7 @@ public virtual void Info(object message, Exception exception) Write(LogLevel.Info, exception, message); } - public virtual void InfoFormat(IFormatProvider formatProvider, string format, params object[] args) + public virtual void InfoFormat(IFormatProvider? formatProvider, string format, params object[] args) { if (!IsInfoEnabled) return; @@ -227,7 +227,7 @@ public virtual void InfoFormat(IFormatProvider formatProvider, string format, pa WriteFormat(LogLevel.Info, formatProvider, format, args); } - public virtual void InfoFormat(IFormatProvider formatProvider, string format, Exception exception, params object[] args) + public virtual void InfoFormat(IFormatProvider? formatProvider, string format, Exception? exception, params object[] args) { if (!IsInfoEnabled) return; @@ -243,7 +243,7 @@ public virtual void InfoFormat(string format, params object[] args) WriteFormat(LogLevel.Info, format, args); } - public virtual void InfoFormat(string format, Exception exception, params object[] args) + public virtual void InfoFormat(string format, Exception? exception, params object[] args) { if (!IsInfoEnabled) return; @@ -299,7 +299,7 @@ public virtual void Warn(object message, Exception exception) Write(LogLevel.Warn, exception, message); } - public virtual void WarnFormat(IFormatProvider formatProvider, string format, params object[] args) + public virtual void WarnFormat(IFormatProvider? formatProvider, string format, params object[] args) { if (!IsWarnEnabled) return; @@ -307,7 +307,7 @@ public virtual void WarnFormat(IFormatProvider formatProvider, string format, pa WriteFormat(LogLevel.Warn, formatProvider, format, args); } - public virtual void WarnFormat(IFormatProvider formatProvider, string format, Exception exception, params object[] args) + public virtual void WarnFormat(IFormatProvider? formatProvider, string format, Exception? exception, params object[] args) { if (!IsWarnEnabled) return; @@ -323,7 +323,7 @@ public virtual void WarnFormat(string format, params object[] args) WriteFormat(LogLevel.Warn, format, args); } - public virtual void WarnFormat(string format, Exception exception, params object[] args) + public virtual void WarnFormat(string format, Exception? exception, params object[] args) { if (!IsWarnEnabled) return; @@ -379,7 +379,7 @@ public virtual void Error(object message, Exception exception) Write(LogLevel.Error, exception, message); } - public virtual void ErrorFormat(IFormatProvider formatProvider, string format, params object[] args) + public virtual void ErrorFormat(IFormatProvider? formatProvider, string format, params object[] args) { if (!IsErrorEnabled) return; @@ -387,7 +387,7 @@ public virtual void ErrorFormat(IFormatProvider formatProvider, string format, p WriteFormat(LogLevel.Error, formatProvider, format, args); } - public virtual void ErrorFormat(IFormatProvider formatProvider, string format, Exception exception, params object[] args) + public virtual void ErrorFormat(IFormatProvider? formatProvider, string format, Exception? exception, params object[] args) { if (!IsErrorEnabled) return; @@ -403,7 +403,7 @@ public virtual void ErrorFormat(string format, params object[] args) WriteFormat(LogLevel.Error, format, args); } - public virtual void ErrorFormat(string format, Exception exception, params object[] args) + public virtual void ErrorFormat(string format, Exception? exception, params object[] args) { if (!IsErrorEnabled) return; @@ -459,7 +459,7 @@ public virtual void Fatal(object message, Exception exception) Write(LogLevel.Fatal, exception, message); } - public virtual void FatalFormat(IFormatProvider formatProvider, string format, params object[] args) + public virtual void FatalFormat(IFormatProvider? formatProvider, string format, params object[] args) { if (!IsFatalEnabled) return; @@ -467,7 +467,7 @@ public virtual void FatalFormat(IFormatProvider formatProvider, string format, p WriteFormat(LogLevel.Fatal, formatProvider, format, args); } - public virtual void FatalFormat(IFormatProvider formatProvider, string format, Exception exception, params object[] args) + public virtual void FatalFormat(IFormatProvider? formatProvider, string format, Exception? exception, params object[] args) { if (!IsFatalEnabled) return; @@ -483,7 +483,7 @@ public virtual void FatalFormat(string format, params object[] args) WriteFormat(LogLevel.Fatal, format, args); } - public virtual void FatalFormat(string format, Exception exception, params object[] args) + public virtual void FatalFormat(string format, Exception? exception, params object[] args) { if (!IsFatalEnabled) return; @@ -543,7 +543,7 @@ protected void Write(LogLevel level, object message) Write(level, null, message); } - protected void Write(LogLevel level, Exception exception, object message) + protected void Write(LogLevel level, Exception? exception, object message) { if (message is string) _logger.Write(level.ToSerilogEventLevel(), exception, "{Message:l}", message.ToString()); @@ -551,21 +551,21 @@ protected void Write(LogLevel level, Exception exception, object message) _logger.Write(level.ToSerilogEventLevel(), exception, "{@Message}", message); } - protected void WriteCallback(LogLevel level, Action formatMessageCallback, Exception exception = null) + protected void WriteCallback(LogLevel level, Action formatMessageCallback, Exception? exception = null) { WriteCallback(level, null, formatMessageCallback, exception); } protected void WriteCallback( LogLevel level, - IFormatProvider formatProvider, + IFormatProvider? formatProvider, Action formatMessageCallback, - Exception exception = null) + Exception? exception = null) { formatMessageCallback(MakeFormatted(level, formatProvider, exception)); } - protected FormatMessageHandler MakeFormatted(LogLevel level, IFormatProvider formatProvider, Exception exception) + protected FormatMessageHandler MakeFormatted(LogLevel level, IFormatProvider? formatProvider, Exception? exception) { var messageHandler = new FormatMessageHandler( delegate(string message, object[] parameters) @@ -588,12 +588,12 @@ protected FormatMessageHandler MakeFormatted(LogLevel level, IFormatProvider for return messageHandler; } - protected void WriteFormat(LogLevel level, Exception exception, string message, object[] parameters) + protected void WriteFormat(LogLevel level, Exception? exception, string message, object[] parameters) { WriteFormat(level, exception, null, message, parameters); } - protected void WriteFormat(LogLevel level, IFormatProvider formatProvider, string message, object[] parameters) + protected void WriteFormat(LogLevel level, IFormatProvider? formatProvider, string message, object[] parameters) { WriteFormat(level, null, formatProvider, message, parameters); } @@ -603,7 +603,7 @@ protected void WriteFormat(LogLevel level, string message, object[] parameters) WriteFormat(level, null, null, message, parameters); } - protected void WriteFormat(LogLevel level, Exception exception, IFormatProvider formatProvider, string message, object[] parameters) + protected void WriteFormat(LogLevel level, Exception? exception, IFormatProvider? formatProvider, string message, object[] parameters) { if (formatProvider == null) { diff --git a/src/SerilogInstanceWrapper.cs b/src/SerilogInstanceWrapper.cs index de8e9c0..62e426f 100644 --- a/src/SerilogInstanceWrapper.cs +++ b/src/SerilogInstanceWrapper.cs @@ -13,8 +13,6 @@ // limitations under the License. -using System; -using System.Collections.Generic; using System.Diagnostics; using Serilog; @@ -25,11 +23,11 @@ namespace Common.Logging.Serilog { internal class SerilogInstanceWrapper : ILogger { - private ILogger _logger; + private ILogger? _logger; private readonly Func _configureContext; - public SerilogInstanceWrapper(Func configureContext, ILogger specificInstance = null) + public SerilogInstanceWrapper(Func configureContext, ILogger? specificInstance = null) { _configureContext = configureContext; if (specificInstance != null) @@ -42,7 +40,7 @@ private ILogger Logger { get { - if (_logger == null && Log.Logger != null && !Log.Logger.ToString().EndsWith("SilentLogger")) + if (_logger == null && !Log.Logger.ToString().EndsWith("SilentLogger")) { // if we actually have a global logger -- let's configure and use it. _logger = _configureContext(Log.Logger); @@ -68,7 +66,7 @@ public ILogger ForContext(IEnumerable enrichers) return Logger.ForContext(enrichers); } - public ILogger ForContext(string propertyName, object value, bool destructureObjects = false) + public ILogger ForContext(string propertyName, object? value, bool destructureObjects = false) { return Logger.ForContext(propertyName, value, destructureObjects); } @@ -109,34 +107,34 @@ public void Write(LogEventLevel level, string messageTemplate, T0 pr Logger.Write(level, messageTemplate, propertyValue0, propertyValue1, propertyValue2); } - public void Write(LogEventLevel level, string messageTemplate, params object[] propertyValues) + public void Write(LogEventLevel level, string messageTemplate, params object?[]? propertyValues) { Logger.Write(level, messageTemplate, propertyValues); } - public void Write(LogEventLevel level, Exception exception, string messageTemplate) + public void Write(LogEventLevel level, Exception? exception, string messageTemplate) { Logger.Write(level, exception, messageTemplate); } - public void Write(LogEventLevel level, Exception exception, string messageTemplate, T propertyValue) + public void Write(LogEventLevel level, Exception? exception, string messageTemplate, T propertyValue) { Logger.Write(level, exception, messageTemplate, propertyValue); } - public void Write(LogEventLevel level, Exception exception, string messageTemplate, T0 propertyValue0, + public void Write(LogEventLevel level, Exception? exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) { Logger.Write(level, exception, messageTemplate, propertyValue0, propertyValue1); } - public void Write(LogEventLevel level, Exception exception, string messageTemplate, T0 propertyValue0, + public void Write(LogEventLevel level, Exception? exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, T2 propertyValue2) { Logger.Write(level, exception, messageTemplate, propertyValue0, propertyValue1, propertyValue2); } - public void Write(LogEventLevel level, Exception exception, string messageTemplate, params object[] propertyValues) + public void Write(LogEventLevel level, Exception? exception, string messageTemplate, params object?[]? propertyValues) { Logger.Write(level, exception, messageTemplate, propertyValues); } @@ -166,33 +164,33 @@ public void Verbose(string messageTemplate, T0 propertyValue0, T1 pr Logger.Verbose(messageTemplate, propertyValue0, propertyValue1, propertyValue2); } - public void Verbose(string messageTemplate, params object[] propertyValues) + public void Verbose(string messageTemplate, params object?[]? propertyValues) { Logger.Verbose(messageTemplate, propertyValues); } - public void Verbose(Exception exception, string messageTemplate) + public void Verbose(Exception? exception, string messageTemplate) { Logger.Verbose(exception, messageTemplate); } - public void Verbose(Exception exception, string messageTemplate, T propertyValue) + public void Verbose(Exception? exception, string messageTemplate, T propertyValue) { Logger.Verbose(exception, messageTemplate, propertyValue); } - public void Verbose(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) + public void Verbose(Exception? exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) { Logger.Verbose(exception, messageTemplate, propertyValue0, propertyValue1); } - public void Verbose(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, + public void Verbose(Exception? exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, T2 propertyValue2) { Logger.Verbose(exception, messageTemplate, propertyValue0, propertyValue1, propertyValue2); } - public void Verbose(Exception exception, string messageTemplate, params object[] propertyValues) + public void Verbose(Exception? exception, string messageTemplate, params object?[]? propertyValues) { Logger.Verbose(exception, messageTemplate, propertyValues); } @@ -217,33 +215,33 @@ public void Debug(string messageTemplate, T0 propertyValue0, T1 prop Logger.Debug(messageTemplate, propertyValue0, propertyValue1, propertyValue2); } - public void Debug(string messageTemplate, params object[] propertyValues) + public void Debug(string messageTemplate, params object?[]? propertyValues) { Logger.Debug(messageTemplate, propertyValues); } - public void Debug(Exception exception, string messageTemplate) + public void Debug(Exception? exception, string messageTemplate) { Logger.Debug(exception, messageTemplate); } - public void Debug(Exception exception, string messageTemplate, T propertyValue) + public void Debug(Exception? exception, string messageTemplate, T propertyValue) { Logger.Debug(exception, messageTemplate, propertyValue); } - public void Debug(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) + public void Debug(Exception? exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) { Logger.Debug(exception, messageTemplate, propertyValue0, propertyValue1); } - public void Debug(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, + public void Debug(Exception? exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, T2 propertyValue2) { Logger.Debug(exception, messageTemplate, propertyValue0, propertyValue1, propertyValue2); } - public void Debug(Exception exception, string messageTemplate, params object[] propertyValues) + public void Debug(Exception? exception, string messageTemplate, params object?[]? propertyValues) { Logger.Debug(exception, messageTemplate, propertyValues); } @@ -268,33 +266,33 @@ public void Information(string messageTemplate, T0 propertyValue0, T Logger.Information(messageTemplate, propertyValue0, propertyValue1, propertyValue2); } - public void Information(string messageTemplate, params object[] propertyValues) + public void Information(string messageTemplate, params object?[]? propertyValues) { Logger.Information(messageTemplate, propertyValues); } - public void Information(Exception exception, string messageTemplate) + public void Information(Exception? exception, string messageTemplate) { Logger.Information(exception, messageTemplate); } - public void Information(Exception exception, string messageTemplate, T propertyValue) + public void Information(Exception? exception, string messageTemplate, T propertyValue) { Logger.Information(exception, messageTemplate, propertyValue); } - public void Information(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) + public void Information(Exception? exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) { Logger.Information(exception, messageTemplate, propertyValue0, propertyValue1); } - public void Information(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, + public void Information(Exception? exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, T2 propertyValue2) { Logger.Information(exception, messageTemplate, propertyValue0, propertyValue1, propertyValue2); } - public void Information(Exception exception, string messageTemplate, params object[] propertyValues) + public void Information(Exception? exception, string messageTemplate, params object?[]? propertyValues) { Logger.Information(exception, messageTemplate, propertyValues); } @@ -319,33 +317,33 @@ public void Warning(string messageTemplate, T0 propertyValue0, T1 pr Logger.Warning(messageTemplate, propertyValue0, propertyValue1, propertyValue2); } - public void Warning(string messageTemplate, params object[] propertyValues) + public void Warning(string messageTemplate, params object?[]? propertyValues) { Logger.Warning(messageTemplate, propertyValues); } - public void Warning(Exception exception, string messageTemplate) + public void Warning(Exception? exception, string messageTemplate) { Logger.Warning(exception, messageTemplate); } - public void Warning(Exception exception, string messageTemplate, T propertyValue) + public void Warning(Exception? exception, string messageTemplate, T propertyValue) { Logger.Warning(exception, messageTemplate, propertyValue); } - public void Warning(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) + public void Warning(Exception? exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) { Logger.Warning(exception, messageTemplate, propertyValue0, propertyValue1); } - public void Warning(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, + public void Warning(Exception? exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, T2 propertyValue2) { Logger.Warning(exception, messageTemplate, propertyValue0, propertyValue1, propertyValue2); } - public void Warning(Exception exception, string messageTemplate, params object[] propertyValues) + public void Warning(Exception? exception, string messageTemplate, params object?[]? propertyValues) { Logger.Warning(exception, messageTemplate, propertyValues); } @@ -370,33 +368,33 @@ public void Error(string messageTemplate, T0 propertyValue0, T1 prop Logger.Error(messageTemplate, propertyValue0, propertyValue1, propertyValue2); } - public void Error(string messageTemplate, params object[] propertyValues) + public void Error(string messageTemplate, params object?[]? propertyValues) { Logger.Error(messageTemplate, propertyValues); } - public void Error(Exception exception, string messageTemplate) + public void Error(Exception? exception, string messageTemplate) { Logger.Error(exception, messageTemplate); } - public void Error(Exception exception, string messageTemplate, T propertyValue) + public void Error(Exception? exception, string messageTemplate, T propertyValue) { Logger.Error(exception, messageTemplate, propertyValue); } - public void Error(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) + public void Error(Exception? exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) { Logger.Error(exception, messageTemplate, propertyValue0, propertyValue1); } - public void Error(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, + public void Error(Exception? exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, T2 propertyValue2) { Logger.Error(exception, messageTemplate, propertyValue0, propertyValue1, propertyValue2); } - public void Error(Exception exception, string messageTemplate, params object[] propertyValues) + public void Error(Exception? exception, string messageTemplate, params object?[]? propertyValues) { Logger.Error(exception, messageTemplate, propertyValues); } @@ -421,46 +419,46 @@ public void Fatal(string messageTemplate, T0 propertyValue0, T1 prop Logger.Fatal(messageTemplate, propertyValue0, propertyValue1, propertyValue2); } - public void Fatal(string messageTemplate, params object[] propertyValues) + public void Fatal(string messageTemplate, params object?[]? propertyValues) { Logger.Fatal(messageTemplate, propertyValues); } - public void Fatal(Exception exception, string messageTemplate) + public void Fatal(Exception? exception, string messageTemplate) { Logger.Fatal(exception, messageTemplate); } - public void Fatal(Exception exception, string messageTemplate, T propertyValue) + public void Fatal(Exception? exception, string messageTemplate, T propertyValue) { Logger.Fatal(exception, messageTemplate, propertyValue); } - public void Fatal(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) + public void Fatal(Exception? exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) { Logger.Fatal(exception, messageTemplate, propertyValue0, propertyValue1); } - public void Fatal(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, + public void Fatal(Exception? exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, T2 propertyValue2) { Logger.Fatal(exception, messageTemplate, propertyValue0, propertyValue1, propertyValue2); } - public void Fatal(Exception exception, string messageTemplate, params object[] propertyValues) + public void Fatal(Exception? exception, string messageTemplate, params object?[]? propertyValues) { Logger.Fatal(exception, messageTemplate, propertyValues); } - public bool BindMessageTemplate(string messageTemplate, object[] propertyValues, out MessageTemplate parsedTemplate, + public bool BindMessageTemplate(string messageTemplate, object?[]? propertyValues, out MessageTemplate parsedTemplate, out IEnumerable boundProperties) { - return Logger.BindMessageTemplate(messageTemplate, propertyValues, out parsedTemplate, out boundProperties); + return Logger.BindMessageTemplate(messageTemplate, propertyValues, out parsedTemplate!, out boundProperties!); } - public bool BindProperty(string propertyName, object value, bool destructureObjects, out LogEventProperty property) + public bool BindProperty(string? propertyName, object? value, bool destructureObjects, out LogEventProperty property) { - return Logger.BindProperty(propertyName, value, destructureObjects, out property); + return Logger.BindProperty(propertyName, value, destructureObjects, out property!); } } } \ No newline at end of file