From a3ae6d5e5ec7a870282429811cdbbd8efbf61c86 Mon Sep 17 00:00:00 2001 From: Erik Mavrinac Date: Wed, 27 Mar 2024 11:52:11 -0700 Subject: [PATCH 1/3] Expand nullability to 'object message' for logger, loggerextensions. More 'is' conversions. --- src/Directory.Build.props | 1 + .../Appender/RollingFileAppenderTest.cs | 2 +- src/log4net.Tests/Core/FixingTest.cs | 4 +- src/log4net.Tests/Core/StringFormatTest.cs | 110 ++++++++++++++++-- src/log4net.Tests/Util/PatternStringTest.cs | 4 +- src/log4net/Appender/AdoNetAppender.cs | 22 ++-- src/log4net/Appender/RollingFileAppender.cs | 6 +- src/log4net/Appender/TextWriterAppender.cs | 4 +- src/log4net/Config/XmlConfigurator.cs | 2 +- src/log4net/Core/ILogger.cs | 2 +- src/log4net/Core/LocationInfo.cs | 4 +- src/log4net/Core/LogImpl.cs | 20 ++-- src/log4net/Core/LoggingEvent.cs | 8 +- src/log4net/Core/TimeEvaluator.cs | 4 +- src/log4net/ILog.cs | 20 ++-- .../Pattern/AspNetCachePatternConverter.cs | 4 +- .../Pattern/AspNetContextPatternConverter.cs | 2 +- .../Layout/Pattern/AspNetPatternConverter.cs | 2 +- .../Pattern/AspNetRequestPatternConverter.cs | 4 +- .../Pattern/AspNetSessionPatternConverter.cs | 4 +- .../Pattern/PropertyPatternConverter.cs | 2 +- .../StackTraceDetailPatternConverter.cs | 2 +- src/log4net/Layout/XmlLayout.cs | 2 +- src/log4net/ObjectRenderer/RendererMap.cs | 6 +- .../Plugin/RemoteLoggingServerPlugin.cs | 2 +- src/log4net/Repository/Hierarchy/Hierarchy.cs | 6 +- src/log4net/Repository/Hierarchy/Logger.cs | 6 +- .../Repository/LoggerRepositorySkeleton.cs | 4 +- src/log4net/Util/ILogExtensions.cs | 30 ++--- src/log4net/Util/LogLog.cs | 6 +- .../Util/LogicalThreadContextProperties.cs | 4 +- src/log4net/Util/LogicalThreadContextStack.cs | 4 +- src/log4net/Util/ThreadContextStacks.cs | 2 +- src/log4net/Util/WindowsSecurityContext.cs | 2 +- 34 files changed, 198 insertions(+), 109 deletions(-) diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 911ea5cb..5f5b7f96 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -3,5 +3,6 @@ true latest Enable + nullable diff --git a/src/log4net.Tests/Appender/RollingFileAppenderTest.cs b/src/log4net.Tests/Appender/RollingFileAppenderTest.cs index 5c11adc8..3f9ff050 100644 --- a/src/log4net.Tests/Appender/RollingFileAppenderTest.cs +++ b/src/log4net.Tests/Appender/RollingFileAppenderTest.cs @@ -1441,7 +1441,7 @@ private static ILogger CreateLogger(string filename, appender.Encoding = Encoding.ASCII; appender.ErrorHandler = handler; appender.MaxSizeRollBackups = maxSizeRollBackups; - if (lockModel != null) + if (lockModel is not null) { appender.LockingModel = lockModel; } diff --git a/src/log4net.Tests/Core/FixingTest.cs b/src/log4net.Tests/Core/FixingTest.cs index 8c61ed0c..25333ed6 100644 --- a/src/log4net.Tests/Core/FixingTest.cs +++ b/src/log4net.Tests/Core/FixingTest.cs @@ -37,7 +37,7 @@ public void CreateRepository() { bool exists = false; Repository.ILoggerRepository[] repositories = LogManager.GetAllRepositories(); - if (repositories != null) + if (repositories is not null) { foreach (Repository.ILoggerRepository r in repositories) { @@ -54,7 +54,7 @@ public void CreateRepository() } // write-once - if (Thread.CurrentThread.Name == null) + if (Thread.CurrentThread.Name is null) { Thread.CurrentThread.Name = "Log4Net Test thread"; } diff --git a/src/log4net.Tests/Core/StringFormatTest.cs b/src/log4net.Tests/Core/StringFormatTest.cs index 6f7863fe..08c5d592 100644 --- a/src/log4net.Tests/Core/StringFormatTest.cs +++ b/src/log4net.Tests/Core/StringFormatTest.cs @@ -108,7 +108,15 @@ public void TestFormatString() Assert.AreEqual(STRING_FORMAT_ERROR, stringAppender.GetString(), "Test formatting error"); stringAppender.Reset(); - // *** + // *** Nulls + log1.Debug(null); + Assert.AreEqual("", stringAppender.GetString()); + stringAppender.Reset(); + + log1.Debug(null, new Exception("Exception message")); + Assert.AreEqual("System.Exception: Exception message" + Environment.NewLine, stringAppender.GetString()); + stringAppender.Reset(); + log1.InfoFormat("One{0} null", null); Assert.AreEqual("One null", stringAppender.GetString()); stringAppender.Reset(); @@ -197,7 +205,15 @@ public void TestLogFormatApi_Debug() Assert.AreEqual("DEBUG:Before Middle After End", stringAppender.GetString(), "Test formatting with 'en' provider"); stringAppender.Reset(); - // *** + // *** Nulls + log1.Debug(null); + Assert.AreEqual("DEBUG:", stringAppender.GetString()); + stringAppender.Reset(); + + log1.Debug(null, new Exception("Exception message")); + Assert.AreEqual("DEBUG:System.Exception: Exception message" + Environment.NewLine, stringAppender.GetString()); + stringAppender.Reset(); + log1.DebugFormat("One{0} null", null); Assert.AreEqual("DEBUG:One null", stringAppender.GetString()); stringAppender.Reset(); @@ -285,7 +301,15 @@ public void TestLogFormatApi_NoDebug() Assert.AreEqual("", stringAppender.GetString(), "Test formatting with 'en' provider"); stringAppender.Reset(); - // *** + // *** Nulls + log1.Debug(null); + Assert.AreEqual("", stringAppender.GetString()); + stringAppender.Reset(); + + log1.Debug(null, new Exception("Exception message")); + Assert.AreEqual("", stringAppender.GetString()); + stringAppender.Reset(); + log1.DebugFormat("One{0} null", null); Assert.AreEqual("", stringAppender.GetString()); stringAppender.Reset(); @@ -372,7 +396,15 @@ public void TestLogFormatApi_Info() Assert.AreEqual("INFO:Before Middle After End", stringAppender.GetString(), "Test formatting with 'en' provider"); stringAppender.Reset(); - // *** + // *** Nulls + log1.Info(null); + Assert.AreEqual("INFO:", stringAppender.GetString()); + stringAppender.Reset(); + + log1.Info(null, new Exception("Exception message")); + Assert.AreEqual("INFO:System.Exception: Exception message" + Environment.NewLine, stringAppender.GetString()); + stringAppender.Reset(); + log1.InfoFormat("One{0} null", null); Assert.AreEqual("INFO:One null", stringAppender.GetString()); stringAppender.Reset(); @@ -460,7 +492,15 @@ public void TestLogFormatApi_NoInfo() Assert.AreEqual("", stringAppender.GetString(), "Test formatting with 'en' provider"); stringAppender.Reset(); - // *** + // *** Nulls + log1.Info(null); + Assert.AreEqual("", stringAppender.GetString()); + stringAppender.Reset(); + + log1.Info(null, new Exception("Exception message")); + Assert.AreEqual("", stringAppender.GetString()); + stringAppender.Reset(); + log1.InfoFormat("One{0} null", null); Assert.AreEqual("", stringAppender.GetString()); stringAppender.Reset(); @@ -547,7 +587,15 @@ public void TestLogFormatApi_Warn() Assert.AreEqual("WARN:Before Middle After End", stringAppender.GetString(), "Test formatting with 'en' provider"); stringAppender.Reset(); - // *** + // *** Nulls + log1.Warn(null); + Assert.AreEqual("WARN:", stringAppender.GetString()); + stringAppender.Reset(); + + log1.Warn(null, new Exception("Exception message")); + Assert.AreEqual("WARN:System.Exception: Exception message" + Environment.NewLine, stringAppender.GetString()); + stringAppender.Reset(); + log1.WarnFormat("One{0} null", null); Assert.AreEqual("WARN:One null", stringAppender.GetString()); stringAppender.Reset(); @@ -635,7 +683,15 @@ public void TestLogFormatApi_NoWarn() Assert.AreEqual("", stringAppender.GetString(), "Test formatting with 'en' provider"); stringAppender.Reset(); - // *** + // *** Nulls + log1.Warn(null); + Assert.AreEqual("", stringAppender.GetString()); + stringAppender.Reset(); + + log1.Warn(null, new Exception("Exception message")); + Assert.AreEqual("", stringAppender.GetString()); + stringAppender.Reset(); + log1.WarnFormat("One{0} null", null); Assert.AreEqual("", stringAppender.GetString()); stringAppender.Reset(); @@ -722,7 +778,15 @@ public void TestLogFormatApi_Error() Assert.AreEqual("ERROR:Before Middle After End", stringAppender.GetString(), "Test formatting with 'en' provider"); stringAppender.Reset(); - // *** + // *** Nulls + log1.Error(null); + Assert.AreEqual("ERROR:", stringAppender.GetString()); + stringAppender.Reset(); + + log1.Error(null, new Exception("Exception message")); + Assert.AreEqual("ERROR:System.Exception: Exception message" + Environment.NewLine, stringAppender.GetString()); + stringAppender.Reset(); + log1.ErrorFormat("One{0} null", null); Assert.AreEqual("ERROR:One null", stringAppender.GetString()); stringAppender.Reset(); @@ -810,7 +874,15 @@ public void TestLogFormatApi_NoError() Assert.AreEqual("", stringAppender.GetString(), "Test formatting with 'en' provider"); stringAppender.Reset(); - // *** + // *** Nulls + log1.Error(null); + Assert.AreEqual("", stringAppender.GetString()); + stringAppender.Reset(); + + log1.Error(null, new Exception("Exception message")); + Assert.AreEqual("", stringAppender.GetString()); + stringAppender.Reset(); + log1.ErrorFormat("One{0} null", null); Assert.AreEqual("", stringAppender.GetString()); stringAppender.Reset(); @@ -897,7 +969,15 @@ public void TestLogFormatApi_Fatal() Assert.AreEqual("FATAL:Before Middle After End", stringAppender.GetString(), "Test formatting with 'en' provider"); stringAppender.Reset(); - // *** + // *** Nulls + log1.Fatal(null); + Assert.AreEqual("FATAL:", stringAppender.GetString()); + stringAppender.Reset(); + + log1.Fatal(null, new Exception("Exception message")); + Assert.AreEqual("FATAL:System.Exception: Exception message" + Environment.NewLine, stringAppender.GetString()); + stringAppender.Reset(); + log1.FatalFormat("One{0} null", null); Assert.AreEqual("FATAL:One null", stringAppender.GetString()); stringAppender.Reset(); @@ -985,7 +1065,15 @@ public void TestLogFormatApi_NoFatal() Assert.AreEqual("", stringAppender.GetString(), "Test formatting with 'en' provider"); stringAppender.Reset(); - // *** + // *** Nulls + log1.Fatal(null); + Assert.AreEqual("", stringAppender.GetString()); + stringAppender.Reset(); + + log1.Fatal(null, new Exception("Exception message")); + Assert.AreEqual("", stringAppender.GetString()); + stringAppender.Reset(); + log1.FatalFormat("One{0} null", null); Assert.AreEqual("", stringAppender.GetString()); stringAppender.Reset(); diff --git a/src/log4net.Tests/Util/PatternStringTest.cs b/src/log4net.Tests/Util/PatternStringTest.cs index e76c9f7c..be5bca53 100644 --- a/src/log4net.Tests/Util/PatternStringTest.cs +++ b/src/log4net.Tests/Util/PatternStringTest.cs @@ -75,11 +75,11 @@ public void TestAppSettingPathConverter() } finally { - if (appDomain != null) + if (appDomain is not null) { AppDomain.Unload(appDomain); } - if (configurationFileName != null) + if (configurationFileName is not null) { File.Delete(configurationFileName); } diff --git a/src/log4net/Appender/AdoNetAppender.cs b/src/log4net/Appender/AdoNetAppender.cs index b52361f2..072fff2c 100644 --- a/src/log4net/Appender/AdoNetAppender.cs +++ b/src/log4net/Appender/AdoNetAppender.cs @@ -376,15 +376,15 @@ protected override void OnClose() /// protected override void SendBuffer(LoggingEvent[] events) { - if (ReconnectOnError && (Connection == null || Connection.State != ConnectionState.Open)) + if (ReconnectOnError && (Connection is null || Connection.State != ConnectionState.Open)) { - LogLog.Debug(declaringType, $"Attempting to reconnect to database. Current Connection State: {((Connection == null) ? SystemInfo.NullText : Connection.State.ToString())}"); + LogLog.Debug(declaringType, $"Attempting to reconnect to database. Current Connection State: {((Connection is null) ? SystemInfo.NullText : Connection.State.ToString())}"); InitializeDatabaseConnection(); } // Check that the connection exists and is open - if (Connection != null && Connection.State == ConnectionState.Open) + if (Connection is not null && Connection.State == ConnectionState.Open) { if (UseTransactions) { @@ -462,7 +462,7 @@ protected virtual void SendBuffer(IDbTransaction? dbTran, LoggingEvent[] events) // Set the command type dbCmd.CommandType = CommandType; // Send buffer using the prepared command object - if (dbTran != null) + if (dbTran is not null) { dbCmd.Transaction = dbTran; } @@ -474,7 +474,7 @@ protected virtual void SendBuffer(IDbTransaction? dbTran, LoggingEvent[] events) } catch (Exception) { - if (dbTran != null) + if (dbTran is not null) { // rethrow exception in transaction mode, cuz now transaction is in failed state throw; @@ -503,7 +503,7 @@ protected virtual void SendBuffer(IDbTransaction? dbTran, LoggingEvent[] events) // create a new command using (IDbCommand dbCmd = Connection!.CreateCommand()) { - if (dbTran != null) + if (dbTran is not null) { dbCmd.Transaction = dbTran; } @@ -550,7 +550,7 @@ protected virtual void Prepare(IDbCommand dbCmd) /// protected virtual string GetLogStatement(LoggingEvent logEvent) { - if (Layout == null) + if (Layout is null) { ErrorHandler.Error("AdoNetAppender: No Layout specified."); return ""; @@ -587,7 +587,7 @@ protected virtual IDbConnection CreateConnection(Type connectionType, string con /// A connection string used to connect to the database. protected virtual string ResolveConnectionString(out string connectionStringContext) { - if (ConnectionString != null && ConnectionString.Length > 0) + if (ConnectionString is not null && ConnectionString.Length > 0) { connectionStringContext = "ConnectionString"; return ConnectionString; @@ -596,7 +596,7 @@ protected virtual string ResolveConnectionString(out string connectionStringCont if (!String.IsNullOrEmpty(ConnectionStringName)) { ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings[ConnectionStringName!]; - if (settings != null) + if (settings is not null) { connectionStringContext = "ConnectionStringName"; return settings.ConnectionString; @@ -607,7 +607,7 @@ protected virtual string ResolveConnectionString(out string connectionStringCont } } - if (AppSettingsKey != null && AppSettingsKey.Length > 0) + if (AppSettingsKey is not null && AppSettingsKey.Length > 0) { connectionStringContext = "AppSettingsKey"; string? appSettingsConnectionString = SystemInfo.GetAppSetting(AppSettingsKey); @@ -694,7 +694,7 @@ private void InitializeDatabaseConnection() /// private void DisposeConnection() { - if (Connection != null) + if (Connection is not null) { try { diff --git a/src/log4net/Appender/RollingFileAppender.cs b/src/log4net/Appender/RollingFileAppender.cs index 5ab30987..ef8de20e 100644 --- a/src/log4net/Appender/RollingFileAppender.cs +++ b/src/log4net/Appender/RollingFileAppender.cs @@ -977,7 +977,7 @@ public RollPoint ComputeCheckPeriod(string datePattern) /// public override void ActivateOptions() { - if (m_rollDate && DatePattern != null) + if (m_rollDate && DatePattern is not null) { m_now = DateTimeStrategy.Now; m_rollPoint = ComputeCheckPeriod(DatePattern); @@ -1018,7 +1018,7 @@ public override void ActivateOptions() .Replace("/", "_") + "_rolling" ); - if (m_rollDate && File != null && m_scheduledFilename == null) + if (m_rollDate && File is not null && m_scheduledFilename is null) { m_scheduledFilename = CombinePath(File, m_now.ToString(DatePattern, DateTimeFormatInfo.InvariantInfo)); } @@ -1058,7 +1058,7 @@ protected void RollOverTime(bool fileIsOpen) if (StaticLogFileName) { // Compute filename, but only if datePattern is specified - if (DatePattern == null) + if (DatePattern is null) { ErrorHandler.Error("Missing DatePattern option in rollOver()."); return; diff --git a/src/log4net/Appender/TextWriterAppender.cs b/src/log4net/Appender/TextWriterAppender.cs index a521ea8a..f0d0dd88 100644 --- a/src/log4net/Appender/TextWriterAppender.cs +++ b/src/log4net/Appender/TextWriterAppender.cs @@ -168,12 +168,12 @@ protected override bool PreAppendCheck() return false; } - if (QuietWriter == null) + if (QuietWriter is null) { // Allow subclass to lazily create the writer PrepareWriter(); - if (QuietWriter == null) + if (QuietWriter is null) { ErrorHandler.Error("No output stream or file set for the appender named [" + Name + "]."); return false; diff --git a/src/log4net/Config/XmlConfigurator.cs b/src/log4net/Config/XmlConfigurator.cs index 29ecbabd..0ed64647 100644 --- a/src/log4net/Config/XmlConfigurator.cs +++ b/src/log4net/Config/XmlConfigurator.cs @@ -690,7 +690,7 @@ private static void InternalConfigureAndWatch(ILoggerRepository repository, File { LogLog.Debug(declaringType, $"configuring repository [{repository.Name}] using file [{configFile}] watching for file updates"); - if (configFile == null) + if (configFile is null) { LogLog.Error(declaringType, "ConfigureAndWatch called with null 'configFile' parameter"); } diff --git a/src/log4net/Core/ILogger.cs b/src/log4net/Core/ILogger.cs index fc6d9436..fec672c4 100644 --- a/src/log4net/Core/ILogger.cs +++ b/src/log4net/Core/ILogger.cs @@ -68,7 +68,7 @@ public interface ILogger /// the and . /// /// - void Log(Type callerStackBoundaryDeclaringType, Level? level, object message, Exception? exception); + void Log(Type callerStackBoundaryDeclaringType, Level? level, object? message, Exception? exception); /// /// This is the most generic printing method that is intended to be used diff --git a/src/log4net/Core/LocationInfo.cs b/src/log4net/Core/LocationInfo.cs index 3e61ea80..f8599603 100644 --- a/src/log4net/Core/LocationInfo.cs +++ b/src/log4net/Core/LocationInfo.cs @@ -91,7 +91,7 @@ public LocationInfo(Type? callerStackBoundaryDeclaringType) while (frameIndex < st.FrameCount) { StackFrame frame = st.GetFrame(frameIndex); - if (frame != null && frame.GetMethod().DeclaringType == callerStackBoundaryDeclaringType) + if (frame is not null && frame.GetMethod().DeclaringType == callerStackBoundaryDeclaringType) { break; } @@ -102,7 +102,7 @@ public LocationInfo(Type? callerStackBoundaryDeclaringType) while (frameIndex < st.FrameCount) { StackFrame frame = st.GetFrame(frameIndex); - if (frame != null && frame.GetMethod().DeclaringType != callerStackBoundaryDeclaringType) + if (frame is not null && frame.GetMethod().DeclaringType != callerStackBoundaryDeclaringType) { break; } diff --git a/src/log4net/Core/LogImpl.cs b/src/log4net/Core/LogImpl.cs index fe927aa0..4c1f39cd 100644 --- a/src/log4net/Core/LogImpl.cs +++ b/src/log4net/Core/LogImpl.cs @@ -162,7 +162,7 @@ protected virtual void ReloadLevels(ILoggerRepository repository) /// form instead. /// /// - public virtual void Debug(object message) + public virtual void Debug(object? message) { Logger.Log(ThisDeclaringType, m_levelDebug, message, null); } @@ -183,7 +183,7 @@ public virtual void Debug(object message) /// /// /// - public virtual void Debug(object message, Exception? exception) + public virtual void Debug(object? message, Exception? exception) { Logger.Log(ThisDeclaringType, m_levelDebug, message, exception); } @@ -360,7 +360,7 @@ public virtual void DebugFormat(IFormatProvider? provider, string format, params /// form instead. /// /// - public virtual void Info(object message) + public virtual void Info(object? message) { Logger.Log(ThisDeclaringType, m_levelInfo, message, null); } @@ -381,7 +381,7 @@ public virtual void Info(object message) /// /// /// - public virtual void Info(object message, Exception? exception) + public virtual void Info(object? message, Exception? exception) { Logger.Log(ThisDeclaringType, m_levelInfo, message, exception); } @@ -558,7 +558,7 @@ public virtual void InfoFormat(IFormatProvider? provider, string format, params /// form instead. /// /// - public virtual void Warn(object message) + public virtual void Warn(object? message) { Logger.Log(ThisDeclaringType, m_levelWarn, message, null); } @@ -579,7 +579,7 @@ public virtual void Warn(object message) /// /// /// - public virtual void Warn(object message, Exception? exception) + public virtual void Warn(object? message, Exception? exception) { Logger.Log(ThisDeclaringType, m_levelWarn, message, exception); } @@ -756,7 +756,7 @@ public virtual void WarnFormat(IFormatProvider? provider, string format, params /// form instead. /// /// - public virtual void Error(object message) + public virtual void Error(object? message) { Logger.Log(ThisDeclaringType, m_levelError, message, null); } @@ -777,7 +777,7 @@ public virtual void Error(object message) /// /// /// - public virtual void Error(object message, Exception? exception) + public virtual void Error(object? message, Exception? exception) { Logger.Log(ThisDeclaringType, m_levelError, message, exception); } @@ -954,7 +954,7 @@ public virtual void ErrorFormat(IFormatProvider? provider, string format, params /// form instead. /// /// - public virtual void Fatal(object message) + public virtual void Fatal(object? message) { Logger.Log(ThisDeclaringType, m_levelFatal, message, null); } @@ -975,7 +975,7 @@ public virtual void Fatal(object message) /// /// /// - public virtual void Fatal(object message, Exception? exception) + public virtual void Fatal(object? message, Exception? exception) { Logger.Log(ThisDeclaringType, m_levelFatal, message, exception); } diff --git a/src/log4net/Core/LoggingEvent.cs b/src/log4net/Core/LoggingEvent.cs index bbc62454..e80eb3e4 100644 --- a/src/log4net/Core/LoggingEvent.cs +++ b/src/log4net/Core/LoggingEvent.cs @@ -580,7 +580,7 @@ public virtual string? RenderedMessage { if (m_data.Message is null && m_cacheUpdatable) { - if (m_message == null) + if (m_message is null) { m_data.Message = string.Empty; } @@ -659,7 +659,7 @@ public string? ThreadName { get { - if (m_data.ThreadName == null && m_cacheUpdatable) + if (m_data.ThreadName is null && m_cacheUpdatable) { // '.NET ThreadPool Worker' appears as a default thread name in the .NET 6-7 thread pool. // '.NET TP Worker' is the default thread name in the .NET 8+ thread pool. @@ -1351,12 +1351,12 @@ private void CacheProperties() /// public PropertiesDictionary GetProperties() { - if (m_data.Properties != null) + if (m_data.Properties is not null) { return m_data.Properties; } - if (m_compositeProperties == null) + if (m_compositeProperties is null) { CreateCompositeProperties(); } diff --git a/src/log4net/Core/TimeEvaluator.cs b/src/log4net/Core/TimeEvaluator.cs index af93e55f..f1ecd04b 100644 --- a/src/log4net/Core/TimeEvaluator.cs +++ b/src/log4net/Core/TimeEvaluator.cs @@ -121,9 +121,9 @@ public int Interval /// public bool IsTriggeringEvent(LoggingEvent loggingEvent) { - if (loggingEvent == null) + if (loggingEvent is null) { - throw new ArgumentNullException("loggingEvent"); + throw new ArgumentNullException(nameof(loggingEvent)); } // disable the evaluator if threshold is zero diff --git a/src/log4net/ILog.cs b/src/log4net/ILog.cs index 946dbe7e..e9c17cdd 100644 --- a/src/log4net/ILog.cs +++ b/src/log4net/ILog.cs @@ -89,7 +89,7 @@ public interface ILog : ILoggerWrapper /// /// /// - void Debug(object message); + void Debug(object? message); /// /// Log a message object with the level including @@ -105,7 +105,7 @@ public interface ILog : ILoggerWrapper /// /// /// - void Debug(object message, Exception? exception); + void Debug(object? message, Exception? exception); /// Log a formatted string with the level. /// @@ -242,7 +242,7 @@ public interface ILog : ILoggerWrapper /// The message object to log. /// /// - void Info(object message); + void Info(object? message); /// /// Logs a message object with the INFO level including @@ -258,7 +258,7 @@ public interface ILog : ILoggerWrapper /// /// /// - void Info(object message, Exception? exception); + void Info(object? message, Exception? exception); /// Log a formatted message string with the level. /// @@ -395,7 +395,7 @@ public interface ILog : ILoggerWrapper /// The message object to log. /// /// - void Warn(object message); + void Warn(object? message); /// /// Log a message object with the level including @@ -411,7 +411,7 @@ public interface ILog : ILoggerWrapper /// /// /// - void Warn(object message, Exception? exception); + void Warn(object? message, Exception? exception); /// Log a formatted message string with the level. /// @@ -548,7 +548,7 @@ public interface ILog : ILoggerWrapper /// /// /// - void Error(object message); + void Error(object? message); /// /// Log a message object with the level including @@ -564,7 +564,7 @@ public interface ILog : ILoggerWrapper /// /// /// - void Error(object message, Exception? exception); + void Error(object? message, Exception? exception); /// Log a formatted message string with the level. /// @@ -701,7 +701,7 @@ public interface ILog : ILoggerWrapper /// The message object to log. /// /// - void Fatal(object message); + void Fatal(object? message); /// /// Log a message object with the level including @@ -717,7 +717,7 @@ public interface ILog : ILoggerWrapper /// /// /// - void Fatal(object message, Exception? exception); + void Fatal(object? message, Exception? exception); /// Log a formatted message string with the level. /// diff --git a/src/log4net/Layout/Pattern/AspNetCachePatternConverter.cs b/src/log4net/Layout/Pattern/AspNetCachePatternConverter.cs index f87020b8..c634a9d6 100644 --- a/src/log4net/Layout/Pattern/AspNetCachePatternConverter.cs +++ b/src/log4net/Layout/Pattern/AspNetCachePatternConverter.cs @@ -52,9 +52,9 @@ internal sealed class AspNetCachePatternConverter : AspNetPatternLayoutConverter /// protected override void Convert(TextWriter writer, LoggingEvent loggingEvent, HttpContext httpContext) { - if (HttpRuntime.Cache != null) + if (HttpRuntime.Cache is not null) { - if (Option != null) + if (Option is not null) { WriteObject(writer, loggingEvent.Repository, HttpRuntime.Cache[Option]); } diff --git a/src/log4net/Layout/Pattern/AspNetContextPatternConverter.cs b/src/log4net/Layout/Pattern/AspNetContextPatternConverter.cs index e2253e12..6c06f71e 100644 --- a/src/log4net/Layout/Pattern/AspNetContextPatternConverter.cs +++ b/src/log4net/Layout/Pattern/AspNetContextPatternConverter.cs @@ -50,7 +50,7 @@ internal sealed class AspNetContextPatternConverter : AspNetPatternLayoutConvert /// protected override void Convert(TextWriter writer, LoggingEvent loggingEvent, HttpContext httpContext) { - if (Option != null) + if (Option is not null) { WriteObject(writer, loggingEvent.Repository, httpContext.Items[Option]); } diff --git a/src/log4net/Layout/Pattern/AspNetPatternConverter.cs b/src/log4net/Layout/Pattern/AspNetPatternConverter.cs index f43dccc7..a59873cd 100644 --- a/src/log4net/Layout/Pattern/AspNetPatternConverter.cs +++ b/src/log4net/Layout/Pattern/AspNetPatternConverter.cs @@ -38,7 +38,7 @@ internal abstract class AspNetPatternLayoutConverter : PatternLayoutConverter { protected override void Convert(TextWriter writer, LoggingEvent loggingEvent) { - if (HttpContext.Current == null) + if (HttpContext.Current is null) { writer.Write(SystemInfo.NotAvailableText); } diff --git a/src/log4net/Layout/Pattern/AspNetRequestPatternConverter.cs b/src/log4net/Layout/Pattern/AspNetRequestPatternConverter.cs index 01c23a02..342be81f 100644 --- a/src/log4net/Layout/Pattern/AspNetRequestPatternConverter.cs +++ b/src/log4net/Layout/Pattern/AspNetRequestPatternConverter.cs @@ -64,9 +64,9 @@ protected override void Convert(TextWriter writer, LoggingEvent loggingEvent, Ht // property returning null } - if (request != null) + if (request is not null) { - if (Option != null) + if (Option is not null) { WriteObject(writer, loggingEvent.Repository, httpContext.Request.Params[Option]); } diff --git a/src/log4net/Layout/Pattern/AspNetSessionPatternConverter.cs b/src/log4net/Layout/Pattern/AspNetSessionPatternConverter.cs index 14f79973..c666a85b 100644 --- a/src/log4net/Layout/Pattern/AspNetSessionPatternConverter.cs +++ b/src/log4net/Layout/Pattern/AspNetSessionPatternConverter.cs @@ -52,9 +52,9 @@ internal sealed class AspNetSessionPatternConverter : AspNetPatternLayoutConvert /// protected override void Convert(TextWriter writer, LoggingEvent loggingEvent, HttpContext httpContext) { - if (httpContext.Session != null) + if (httpContext.Session is not null) { - if (Option != null) + if (Option is not null) { WriteObject(writer, loggingEvent.Repository, httpContext.Session.Contents[Option]); } diff --git a/src/log4net/Layout/Pattern/PropertyPatternConverter.cs b/src/log4net/Layout/Pattern/PropertyPatternConverter.cs index e1b8f61c..4a88e931 100644 --- a/src/log4net/Layout/Pattern/PropertyPatternConverter.cs +++ b/src/log4net/Layout/Pattern/PropertyPatternConverter.cs @@ -58,7 +58,7 @@ internal sealed class PropertyPatternConverter : PatternLayoutConverter /// protected override void Convert(TextWriter writer, LoggingEvent loggingEvent) { - if (Option != null) + if (Option is not null) { // Write the value for the specified key WriteObject(writer, loggingEvent.Repository, loggingEvent.LookupProperty(Option)); diff --git a/src/log4net/Layout/Pattern/StackTraceDetailPatternConverter.cs b/src/log4net/Layout/Pattern/StackTraceDetailPatternConverter.cs index 862a619c..aa3dea5d 100644 --- a/src/log4net/Layout/Pattern/StackTraceDetailPatternConverter.cs +++ b/src/log4net/Layout/Pattern/StackTraceDetailPatternConverter.cs @@ -47,7 +47,7 @@ internal override string GetMethodInformation(MethodItem method) string param = ""; string[] names = method.Parameters; StringBuilder sb = new StringBuilder(); - if (names != null && names.GetUpperBound(0) > 0) + if (names is not null && names.GetUpperBound(0) > 0) { for (int i = 0; i <= names.GetUpperBound(0); i++) { diff --git a/src/log4net/Layout/XmlLayout.cs b/src/log4net/Layout/XmlLayout.cs index 6ce3cdab..cf8c9a4a 100644 --- a/src/log4net/Layout/XmlLayout.cs +++ b/src/log4net/Layout/XmlLayout.cs @@ -266,7 +266,7 @@ protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent) } string? exceptionStr = loggingEvent.GetExceptionString(); - if (exceptionStr != null && exceptionStr.Length > 0) + if (exceptionStr is not null && exceptionStr.Length > 0) { // Append the stack trace line writer.WriteStartElement(m_elmException, Prefix, ELM_EXCEPTION, Prefix); diff --git a/src/log4net/ObjectRenderer/RendererMap.cs b/src/log4net/ObjectRenderer/RendererMap.cs index 13e05dfa..0724e71b 100644 --- a/src/log4net/ObjectRenderer/RendererMap.cs +++ b/src/log4net/ObjectRenderer/RendererMap.cs @@ -171,11 +171,11 @@ public IObjectRenderer Get(Type type) // Check cache if (!m_cache.TryGetValue(type, out IObjectRenderer? result)) { - for (Type? cur = type; cur != null; cur = cur.BaseType) + for (Type? cur = type; cur is not null; cur = cur.BaseType) { // Search the type's interfaces result = SearchTypeAndInterfaces(cur); - if (result != null) + if (result is not null) { break; } @@ -206,7 +206,7 @@ public IObjectRenderer Get(Type type) foreach (Type t in type.GetInterfaces()) { r = SearchTypeAndInterfaces(t); - if (r != null) + if (r is not null) { return r; } diff --git a/src/log4net/Plugin/RemoteLoggingServerPlugin.cs b/src/log4net/Plugin/RemoteLoggingServerPlugin.cs index 57f904bd..adb1f555 100644 --- a/src/log4net/Plugin/RemoteLoggingServerPlugin.cs +++ b/src/log4net/Plugin/RemoteLoggingServerPlugin.cs @@ -195,7 +195,7 @@ public void LogEvents(LoggingEvent[]? events) { foreach (LoggingEvent logEvent in events) { - if (logEvent != null) + if (logEvent is not null) { m_repository.Log(logEvent); } diff --git a/src/log4net/Repository/Hierarchy/Hierarchy.cs b/src/log4net/Repository/Hierarchy/Hierarchy.cs index 56dd2963..8159d128 100644 --- a/src/log4net/Repository/Hierarchy/Hierarchy.cs +++ b/src/log4net/Repository/Hierarchy/Hierarchy.cs @@ -309,7 +309,7 @@ public override ILogger[] GetCurrentLoggers() /// The logger object with the name specified public override ILogger GetLogger(string name) { - if (name == null) + if (name is null) { throw new ArgumentNullException(nameof(name)); } @@ -649,11 +649,11 @@ public void Clear() /// public Logger GetLogger(string name, ILoggerFactory factory) { - if (name == null) + if (name is null) { throw new ArgumentNullException(nameof(name)); } - if (factory == null) + if (factory is null) { throw new ArgumentNullException(nameof(factory)); } diff --git a/src/log4net/Repository/Hierarchy/Logger.cs b/src/log4net/Repository/Hierarchy/Logger.cs index e04da5bd..b97cd643 100644 --- a/src/log4net/Repository/Hierarchy/Logger.cs +++ b/src/log4net/Repository/Hierarchy/Logger.cs @@ -350,7 +350,7 @@ public virtual void RemoveAllAppenders() /// This method must not throw any exception to the caller. /// /// - public virtual void Log(Type? callerStackBoundaryDeclaringType, Level? level, object message, Exception? exception) + public virtual void Log(Type? callerStackBoundaryDeclaringType, Level? level, object? message, Exception? exception) { try { @@ -555,7 +555,7 @@ public virtual void CloseNestedAppenders() /// the . /// /// - public virtual void Log(Level level, object message, Exception? exception) + public virtual void Log(Level level, object? message, Exception? exception) { if (IsEnabledFor(level)) { @@ -577,7 +577,7 @@ public virtual void Log(Level level, object message, Exception? exception) /// appenders. /// /// - protected virtual void ForcedLog(Type callerStackBoundaryDeclaringType, Level? level, object message, Exception? exception) + protected virtual void ForcedLog(Type callerStackBoundaryDeclaringType, Level? level, object? message, Exception? exception) { CallAppenders(new LoggingEvent(callerStackBoundaryDeclaringType, Hierarchy, Name, level, message, exception)); } diff --git a/src/log4net/Repository/LoggerRepositorySkeleton.cs b/src/log4net/Repository/LoggerRepositorySkeleton.cs index 6995eea4..b479e247 100644 --- a/src/log4net/Repository/LoggerRepositorySkeleton.cs +++ b/src/log4net/Repository/LoggerRepositorySkeleton.cs @@ -417,11 +417,11 @@ private void AddBuiltinLevels() /// public virtual void AddRenderer(Type typeToRender, IObjectRenderer rendererInstance) { - if (typeToRender == null) + if (typeToRender is null) { throw new ArgumentNullException(nameof(typeToRender)); } - if (rendererInstance == null) + if (rendererInstance is null) { throw new ArgumentNullException(nameof(rendererInstance)); } diff --git a/src/log4net/Util/ILogExtensions.cs b/src/log4net/Util/ILogExtensions.cs index b45150d2..3593442a 100644 --- a/src/log4net/Util/ILogExtensions.cs +++ b/src/log4net/Util/ILogExtensions.cs @@ -106,7 +106,7 @@ public static void DebugExt(this ILog logger, Func callback) /// /// /// - public static void DebugExt(this ILog logger, Func callback, Exception exception) + public static void DebugExt(this ILog logger, Func callback, Exception? exception) { try { @@ -147,7 +147,7 @@ public static void DebugExt(this ILog logger, Func callback, Exception e /// /// /// - public static void DebugExt(this ILog logger, object message) + public static void DebugExt(this ILog logger, object? message) { try { @@ -177,7 +177,7 @@ public static void DebugExt(this ILog logger, object message) /// /// /// - public static void DebugExt(this ILog logger, object message, Exception exception) + public static void DebugExt(this ILog logger, object? message, Exception? exception) { try { @@ -426,7 +426,7 @@ public static void InfoExt(this ILog logger, Func callback) /// /// /// - public static void InfoExt(this ILog logger, Func callback, Exception exception) + public static void InfoExt(this ILog logger, Func callback, Exception? exception) { try { @@ -467,7 +467,7 @@ public static void InfoExt(this ILog logger, Func callback, Exception ex /// /// /// - public static void InfoExt(this ILog logger, object message) + public static void InfoExt(this ILog logger, object? message) { try { @@ -497,7 +497,7 @@ public static void InfoExt(this ILog logger, object message) /// /// /// - public static void InfoExt(this ILog logger, object message, Exception exception) + public static void InfoExt(this ILog logger, object? message, Exception? exception) { try { @@ -746,7 +746,7 @@ public static void WarnExt(this ILog logger, Func callback) /// /// /// - public static void WarnExt(this ILog logger, Func callback, Exception exception) + public static void WarnExt(this ILog logger, Func callback, Exception? exception) { try { @@ -787,7 +787,7 @@ public static void WarnExt(this ILog logger, Func callback, Exception ex /// /// /// - public static void WarnExt(this ILog logger, object message) + public static void WarnExt(this ILog logger, object? message) { try { @@ -817,7 +817,7 @@ public static void WarnExt(this ILog logger, object message) /// /// /// - public static void WarnExt(this ILog logger, object message, Exception exception) + public static void WarnExt(this ILog logger, object? message, Exception? exception) { try { @@ -1066,7 +1066,7 @@ public static void ErrorExt(this ILog logger, Func callback) /// /// /// - public static void ErrorExt(this ILog logger, Func callback, Exception exception) + public static void ErrorExt(this ILog logger, Func callback, Exception? exception) { try { @@ -1107,7 +1107,7 @@ public static void ErrorExt(this ILog logger, Func callback, Exception e /// /// /// - public static void ErrorExt(this ILog logger, object message) + public static void ErrorExt(this ILog logger, object? message) { try { @@ -1137,7 +1137,7 @@ public static void ErrorExt(this ILog logger, object message) /// /// /// - public static void ErrorExt(this ILog logger, object message, Exception exception) + public static void ErrorExt(this ILog logger, object? message, Exception? exception) { try { @@ -1386,7 +1386,7 @@ public static void FatalExt(this ILog logger, Func callback) /// /// /// - public static void FatalExt(this ILog logger, Func callback, Exception exception) + public static void FatalExt(this ILog logger, Func callback, Exception? exception) { try { @@ -1427,7 +1427,7 @@ public static void FatalExt(this ILog logger, Func callback, Exception e /// /// /// - public static void FatalExt(this ILog logger, object message) + public static void FatalExt(this ILog logger, object? message) { try { @@ -1457,7 +1457,7 @@ public static void FatalExt(this ILog logger, object message) /// /// /// - public static void FatalExt(this ILog logger, object message, Exception exception) + public static void FatalExt(this ILog logger, object? message, Exception? exception) { try { diff --git a/src/log4net/Util/LogLog.cs b/src/log4net/Util/LogLog.cs index 03359a5b..1053bfc8 100644 --- a/src/log4net/Util/LogLog.cs +++ b/src/log4net/Util/LogLog.cs @@ -300,7 +300,7 @@ public static void Debug(Type source, string message, Exception? exception) if (EmitInternalMessages) { EmitOutLine(PREFIX + message); - if (exception != null) + if (exception is not null) { EmitOutLine(exception.ToString()); } @@ -363,7 +363,7 @@ public static void Warn(Type source, string message, Exception? exception) if (EmitInternalMessages) { EmitErrorLine(WARN_PREFIX + message); - if (exception != null) + if (exception is not null) { EmitErrorLine(exception.ToString()); } @@ -431,7 +431,7 @@ public static void Error(Type source, string message, Exception? exception) if (EmitInternalMessages) { EmitErrorLine(ERR_PREFIX + message); - if (exception != null) + if (exception is not null) { EmitErrorLine(exception.ToString()); } diff --git a/src/log4net/Util/LogicalThreadContextProperties.cs b/src/log4net/Util/LogicalThreadContextProperties.cs index 49234d56..88c9389f 100644 --- a/src/log4net/Util/LogicalThreadContextProperties.cs +++ b/src/log4net/Util/LogicalThreadContextProperties.cs @@ -111,7 +111,7 @@ public override object? this[string key] public void Remove(string key) { PropertiesDictionary? dictionary = GetProperties(false); - if (dictionary != null) + if (dictionary is not null) { PropertiesDictionary immutableProps = new PropertiesDictionary(dictionary); immutableProps.Remove(key); @@ -155,7 +155,7 @@ public void Clear() try { PropertiesDictionary? properties = GetLogicalProperties(); - if (properties == null && create) + if (properties is null && create) { properties = new PropertiesDictionary(); SetLogicalProperties(properties); diff --git a/src/log4net/Util/LogicalThreadContextStack.cs b/src/log4net/Util/LogicalThreadContextStack.cs index 7c4929b8..78111c65 100644 --- a/src/log4net/Util/LogicalThreadContextStack.cs +++ b/src/log4net/Util/LogicalThreadContextStack.cs @@ -234,7 +234,7 @@ internal StackFrame(string? message, StackFrame? parent) Message = message; m_parent = parent; - if (parent == null) + if (parent is null) { m_fullMessage = message; } @@ -266,7 +266,7 @@ internal string? FullMessage { get { - if (m_fullMessage == null && m_parent != null) + if (m_fullMessage is null && m_parent is not null) { m_fullMessage = string.Concat(m_parent.FullMessage, " ", Message); } diff --git a/src/log4net/Util/ThreadContextStacks.cs b/src/log4net/Util/ThreadContextStacks.cs index e357b307..4bb0c7c2 100644 --- a/src/log4net/Util/ThreadContextStacks.cs +++ b/src/log4net/Util/ThreadContextStacks.cs @@ -55,7 +55,7 @@ public ThreadContextStack this[string key] { // Look for existing stack stack = propertyValue as ThreadContextStack; - if (stack == null) + if (stack is null) { // Property is not set to a stack! string propertyValueString = SystemInfo.NullText; diff --git a/src/log4net/Util/WindowsSecurityContext.cs b/src/log4net/Util/WindowsSecurityContext.cs index aab93dc3..52de78f0 100644 --- a/src/log4net/Util/WindowsSecurityContext.cs +++ b/src/log4net/Util/WindowsSecurityContext.cs @@ -206,7 +206,7 @@ public void ActivateOptions() { if (Credentials == ImpersonationMode.User) { - if (m_identity != null) + if (m_identity is not null) { return new DisposableImpersonationContext(m_identity.Impersonate()); } From b88fdc153d9767860cc2e4434783c61f2d317cf9 Mon Sep 17 00:00:00 2001 From: Erik Mavrinac Date: Wed, 27 Mar 2024 14:48:31 -0700 Subject: [PATCH 2/3] Seal some classes, reduce doc redundancy, reduce some nullables --- .../Appender/DebugAppenderTest.cs | 2 +- .../Appender/SmtpPickupDirAppenderTest.cs | 2 +- .../Core/DefaultRepositorySelectorTest.cs | 2 +- .../AbsoluteTimeDateFormatterTest.cs | 2 +- .../Hierarchy/XmlHierarchyConfiguratorTest.cs | 2 +- src/log4net.Tests/Layout/PatternLayoutTest.cs | 4 +- src/log4net/Appender/AppenderSkeleton.cs | 2 +- src/log4net/Appender/EventLogAppender.cs | 37 +++-------- src/log4net/Core/ILogger.cs | 43 +++---------- src/log4net/Core/LevelMap.cs | 61 ++++--------------- src/log4net/Core/LogImpl.cs | 10 +-- .../StackTraceDetailPatternConverter.cs | 9 +-- .../Layout/Pattern/UtcDatePatternConverter.cs | 2 +- .../Hierarchy/DefaultLoggerFactory.cs | 2 +- src/log4net/Repository/Hierarchy/Hierarchy.cs | 2 +- src/log4net/Repository/Hierarchy/Logger.cs | 8 --- src/log4net/Util/LevelMappingEntry.cs | 25 +------- .../LiteralPatternConverter.cs | 2 +- .../UtcDatePatternConverter.cs | 2 +- .../Util/TypeConverters/BooleanConverter.cs | 2 +- .../Util/TypeConverters/EncodingConverter.cs | 2 +- .../Util/TypeConverters/IPAddressConverter.cs | 2 +- .../TypeConverters/PatternLayoutConverter.cs | 2 +- .../TypeConverters/PatternStringConverter.cs | 2 +- .../Util/TypeConverters/TypeConverter.cs | 2 +- 25 files changed, 61 insertions(+), 170 deletions(-) diff --git a/src/log4net.Tests/Appender/DebugAppenderTest.cs b/src/log4net.Tests/Appender/DebugAppenderTest.cs index cefdc4ba..6749a282 100644 --- a/src/log4net.Tests/Appender/DebugAppenderTest.cs +++ b/src/log4net.Tests/Appender/DebugAppenderTest.cs @@ -138,7 +138,7 @@ public void MethodNameCategoryTest() Debug.Listeners.Remove(categoryTraceListener); } - private class TestErrorHandler : IErrorHandler + private sealed class TestErrorHandler : IErrorHandler { public bool ErrorOccured { get; private set; } diff --git a/src/log4net.Tests/Appender/SmtpPickupDirAppenderTest.cs b/src/log4net.Tests/Appender/SmtpPickupDirAppenderTest.cs index c62571f5..05a1e4c6 100644 --- a/src/log4net.Tests/Appender/SmtpPickupDirAppenderTest.cs +++ b/src/log4net.Tests/Appender/SmtpPickupDirAppenderTest.cs @@ -37,7 +37,7 @@ public class SmtpPickupDirAppenderTest { private readonly string _testPickupDir; - private class SilentErrorHandler : IErrorHandler + private sealed class SilentErrorHandler : IErrorHandler { private readonly StringBuilder m_buffer = new(); diff --git a/src/log4net.Tests/Core/DefaultRepositorySelectorTest.cs b/src/log4net.Tests/Core/DefaultRepositorySelectorTest.cs index a0525927..7697bbb1 100644 --- a/src/log4net.Tests/Core/DefaultRepositorySelectorTest.cs +++ b/src/log4net.Tests/Core/DefaultRepositorySelectorTest.cs @@ -182,5 +182,5 @@ public IAppender[] GetAppenders() } } - internal class MockLoggerRepository2 : MockLoggerRepository; + internal sealed class MockLoggerRepository2 : MockLoggerRepository; } diff --git a/src/log4net.Tests/DateFormatter/AbsoluteTimeDateFormatterTest.cs b/src/log4net.Tests/DateFormatter/AbsoluteTimeDateFormatterTest.cs index 35c68456..1d8752f9 100644 --- a/src/log4net.Tests/DateFormatter/AbsoluteTimeDateFormatterTest.cs +++ b/src/log4net.Tests/DateFormatter/AbsoluteTimeDateFormatterTest.cs @@ -114,7 +114,7 @@ override protected void FormatDateWithoutMillis(DateTime dateToFormat, StringBui } } - internal class FormatterTwo : FormatterOne + internal sealed class FormatterTwo : FormatterOne { } } diff --git a/src/log4net.Tests/Hierarchy/XmlHierarchyConfiguratorTest.cs b/src/log4net.Tests/Hierarchy/XmlHierarchyConfiguratorTest.cs index 9122a86b..94d81cba 100644 --- a/src/log4net.Tests/Hierarchy/XmlHierarchyConfiguratorTest.cs +++ b/src/log4net.Tests/Hierarchy/XmlHierarchyConfiguratorTest.cs @@ -57,7 +57,7 @@ private void SetTestPropWithPath() } // workaround for SetParameter being protected - private class TestConfigurator : XmlHierarchyConfigurator + private sealed class TestConfigurator : XmlHierarchyConfigurator { public TestConfigurator() : base(null!) { diff --git a/src/log4net.Tests/Layout/PatternLayoutTest.cs b/src/log4net.Tests/Layout/PatternLayoutTest.cs index 6af60748..5b5ed8d9 100644 --- a/src/log4net.Tests/Layout/PatternLayoutTest.cs +++ b/src/log4net.Tests/Layout/PatternLayoutTest.cs @@ -310,7 +310,7 @@ public void NamedPatternConverterWithPrecision2ShouldStripLessLeadingStuffIfPres /// /// Converter to include event message /// - private class TestMessagePatternConverter : PatternLayoutConverter + private sealed class TestMessagePatternConverter : PatternLayoutConverter { /// /// Convert the pattern to the rendered message @@ -344,7 +344,7 @@ public void TestExceptionPattern() stringAppender.Reset(); } - private class MessageAsNamePatternConverter : NamedPatternConverter + private sealed class MessageAsNamePatternConverter : NamedPatternConverter { protected override string GetFullyQualifiedName(LoggingEvent loggingEvent) { diff --git a/src/log4net/Appender/AppenderSkeleton.cs b/src/log4net/Appender/AppenderSkeleton.cs index a7ff6c6c..02592718 100644 --- a/src/log4net/Appender/AppenderSkeleton.cs +++ b/src/log4net/Appender/AppenderSkeleton.cs @@ -72,7 +72,7 @@ protected AppenderSkeleton() // There is no point in closing twice. if (!m_closed) { - LogLog.Debug(declaringType, "Finalizing appender named [" + Name + "]."); + LogLog.Debug(declaringType, $"Finalizing appender named [{Name}]."); Close(); } } diff --git a/src/log4net/Appender/EventLogAppender.cs b/src/log4net/Appender/EventLogAppender.cs index 60a53ddf..6ea52021 100644 --- a/src/log4net/Appender/EventLogAppender.cs +++ b/src/log4net/Appender/EventLogAppender.cs @@ -203,11 +203,7 @@ public void AddMapping(Level2EventLogEntryType mapping) /// This property provides the fallback value which defaults to 0. /// /// - public int EventId - { - get { return m_eventId; } - set { m_eventId = value; } - } + public int EventId { get; set; } /// @@ -221,11 +217,7 @@ public int EventId /// This property provides the fallback value which defaults to 0. /// /// - public short Category - { - get { return m_category; } - set { m_category = value; } - } + public short Category { get; set; } /// /// Initialize the appender based on the options set @@ -297,12 +289,11 @@ public override void ActivateOptions() m_levelMapping.ActivateOptions(); - LogLog.Debug(declaringType, "Source [" + ApplicationName + "] is registered to log [" + registeredLogName + "]"); + LogLog.Debug(declaringType, $"Source [{ApplicationName}] is registered to log [{registeredLogName}]"); } catch (System.Security.SecurityException ex) { - ErrorHandler.Error("Caught a SecurityException trying to access the EventLog. Most likely the event source " - + ApplicationName + ErrorHandler.Error($"Caught a SecurityException trying to access the EventLog. Most likely the event source {ApplicationName}" + " doesn't exist and must be created by a local administrator. Will disable EventLogAppender." + " See http://logging.apache.org/log4net/release/faq.html#trouble-EventLog", ex); @@ -315,8 +306,10 @@ public override void ActivateOptions() /// private static void CreateEventSource(string source, string logName, string machineName) { - EventSourceCreationData eventSourceCreationData = new EventSourceCreationData(source, logName); - eventSourceCreationData.MachineName = machineName; + var eventSourceCreationData = new EventSourceCreationData(source, logName) + { + MachineName = machineName + }; EventLog.CreateEventSource(eventSourceCreationData); } @@ -341,7 +334,7 @@ protected override void Append(LoggingEvent loggingEvent) // // Write the resulting string to the event log system // - int eventID = m_eventId; + int eventID = EventId; // Look for the EventID property if (loggingEvent.LookupProperty("EventID") is object eventIDPropertyObj) @@ -371,7 +364,7 @@ protected override void Append(LoggingEvent loggingEvent) } } - short category = m_category; + short category = Category; // Look for the Category property if (loggingEvent.LookupProperty("Category") is object categoryPropertyObj) { @@ -476,16 +469,6 @@ public virtual EventLogEntryType GetEntryType(Level? level) /// private readonly LevelMapping m_levelMapping = new(); - /// - /// The event ID to use unless one is explicitly specified via the LoggingEvent's properties. - /// - private int m_eventId; - - /// - /// The event category to use unless one is explicitly specified via the LoggingEvent's properties. - /// - private short m_category; - /// /// A class to act as a mapping between the level that a logging call is made at and /// the color it should be displayed as. diff --git a/src/log4net/Core/ILogger.cs b/src/log4net/Core/ILogger.cs index fec672c4..d3b513ad 100644 --- a/src/log4net/Core/ILogger.cs +++ b/src/log4net/Core/ILogger.cs @@ -24,15 +24,12 @@ namespace log4net.Core { /// - /// Interface that all loggers implement + /// Interface that all loggers implement to support logging events and testing if a level + /// is enabled for logging. /// /// /// - /// This interface supports logging events and testing if a level - /// is enabled for logging. - /// - /// - /// These methods will not throw exceptions. Note to implementor, ensure + /// These methods will not throw exceptions. Note to implementers, ensure /// that the implementation of these methods cannot allow an exception /// to be thrown to the caller. /// @@ -44,18 +41,11 @@ public interface ILogger /// /// Gets the name of the logger. /// - /// - /// The name of the logger. - /// - /// - /// - /// The name of this logger - /// - /// string Name { get; } /// - /// This generic form is intended to be used by wrappers. + /// Generates a logging event for the specified using + /// the and . /// /// The declaring type of the method that is /// the stack boundary into the logging system for this call. @@ -64,20 +54,19 @@ public interface ILogger /// the exception to log, including its stack trace. Pass null to not log an exception. /// /// - /// Generates a logging event for the specified using - /// the and . + /// This generic form is intended to be used by wrappers. /// /// void Log(Type callerStackBoundaryDeclaringType, Level? level, object? message, Exception? exception); /// - /// This is the most generic printing method that is intended to be used - /// by wrappers. + /// Logs the specified logging event through this logger. /// /// The event being logged. /// /// - /// Logs the specified logging event through this logger. + /// This is the most generic printing method that is intended to be used + /// by wrappers. /// /// void Log(LoggingEvent logEvent); @@ -89,26 +78,12 @@ public interface ILogger /// /// true if this logger is enabled for level, otherwise false. /// - /// - /// - /// Test if this logger is going to log events of the specified . - /// - /// bool IsEnabledFor(Level? level); /// /// Gets the where this /// Logger instance is attached to. /// - /// - /// The that this logger belongs to. - /// - /// - /// - /// Gets the where this - /// Logger instance is attached to. - /// - /// ILoggerRepository? Repository { get; } } } diff --git a/src/log4net/Core/LevelMap.cs b/src/log4net/Core/LevelMap.cs index 2a73caab..45a0c8ed 100644 --- a/src/log4net/Core/LevelMap.cs +++ b/src/log4net/Core/LevelMap.cs @@ -56,17 +56,10 @@ public void Clear() } /// - /// Lookup a by name + /// Looks up a by name /// - /// The name of the Level to lookup - /// a Level from the map with the name specified - /// - /// - /// Returns the from the - /// map with the name specified. If the no level is - /// found then null is returned. - /// - /// + /// The name of the Level to look up. + /// A Level from the map with the name specified, or null if none is found. public Level? this[string name] { get @@ -82,15 +75,10 @@ public Level? this[string name] } /// - /// Create a new Level and add it to the map + /// Creates a new Level and adds it to the map. /// /// the string to display for the Level /// the level value to give to the Level - /// - /// - /// Create a new Level and add it to the map - /// - /// /// public void Add(string name, int value) { @@ -98,16 +86,11 @@ public void Add(string name, int value) } /// - /// Create a new Level and add it to the map + /// Creates a new Level and adds it to the map. /// /// the string to display for the Level /// the level value to give to the Level /// the display name to give to the Level - /// - /// - /// Create a new Level and add it to the map - /// - /// public void Add(string name, int value, string? displayName) { if (name is null) @@ -128,14 +111,9 @@ public void Add(string name, int value, string? displayName) } /// - /// Add a Level to the map + /// Adds a Level to the map. /// /// the Level to add - /// - /// - /// Add a Level to the map - /// - /// public void Add(Level level) { if (level is null) @@ -146,34 +124,21 @@ public void Add(Level level) } /// - /// Return all possible levels as a list of Level objects. + /// Gets all possible levels as a collection of Level objects. /// - /// all possible levels as a list of Level objects - /// - /// - /// Return all possible levels as a list of Level objects. - /// - /// public LevelCollection AllLevels => new(m_mapName2Level.Values); /// - /// Lookup a named level from the map + /// Looks up a named level from the map. /// - /// the name of the level to lookup is taken from this level. - /// If the level is not set on the map then this level is added - /// the level in the map with the name specified - /// - /// - /// Lookup a named level from the map. The name of the level to lookup is taken - /// from the property of the - /// argument. - /// - /// + /// + /// The name of the level to look up is taken from this level. + /// If the level is not set in the map then this level is added. /// If no level with the specified name is found then the /// argument is added to the level map /// and returned. - /// - /// + /// + /// the level in the map with the name specified public Level LookupWithDefault(Level defaultLevel) { if (defaultLevel is null) diff --git a/src/log4net/Core/LogImpl.cs b/src/log4net/Core/LogImpl.cs index 4c1f39cd..282a063c 100644 --- a/src/log4net/Core/LogImpl.cs +++ b/src/log4net/Core/LogImpl.cs @@ -1261,10 +1261,10 @@ private void LoggerRepositoryConfigurationChanged(object sender, EventArgs e) /// private static readonly object?[] OneNullArgs = { null }; - private Level? m_levelDebug; - private Level? m_levelInfo; - private Level? m_levelWarn; - private Level? m_levelError; - private Level? m_levelFatal; + private Level m_levelDebug = Level.Debug; + private Level m_levelInfo = Level.Info; + private Level m_levelWarn = Level.Warn; + private Level m_levelError = Level.Error; + private Level m_levelFatal = Level.Fatal; } } diff --git a/src/log4net/Layout/Pattern/StackTraceDetailPatternConverter.cs b/src/log4net/Layout/Pattern/StackTraceDetailPatternConverter.cs index aa3dea5d..cc5f9d2d 100644 --- a/src/log4net/Layout/Pattern/StackTraceDetailPatternConverter.cs +++ b/src/log4net/Layout/Pattern/StackTraceDetailPatternConverter.cs @@ -27,16 +27,11 @@ namespace log4net.Layout.Pattern { /// - /// Write the caller stack frames to the output - /// - /// - /// /// Writes the to the output writer, using format: /// type3.MethodCall3(type param,...) > type2.MethodCall2(type param,...) > type1.MethodCall1(type param,...) - /// - /// + /// /// Adam Davies - internal class StackTraceDetailPatternConverter : StackTracePatternConverter + internal sealed class StackTraceDetailPatternConverter : StackTracePatternConverter { internal override string GetMethodInformation(MethodItem method) { diff --git a/src/log4net/Layout/Pattern/UtcDatePatternConverter.cs b/src/log4net/Layout/Pattern/UtcDatePatternConverter.cs index 8993a54f..5355242e 100644 --- a/src/log4net/Layout/Pattern/UtcDatePatternConverter.cs +++ b/src/log4net/Layout/Pattern/UtcDatePatternConverter.cs @@ -44,7 +44,7 @@ namespace log4net.Layout.Pattern /// /// /// Nicko Cadell - internal class UtcDatePatternConverter : DatePatternConverter + internal sealed class UtcDatePatternConverter : DatePatternConverter { /// /// Writes the TimeStamp to the output. diff --git a/src/log4net/Repository/Hierarchy/DefaultLoggerFactory.cs b/src/log4net/Repository/Hierarchy/DefaultLoggerFactory.cs index 3aea766b..4d3c9b5e 100644 --- a/src/log4net/Repository/Hierarchy/DefaultLoggerFactory.cs +++ b/src/log4net/Repository/Hierarchy/DefaultLoggerFactory.cs @@ -33,7 +33,7 @@ namespace log4net.Repository.Hierarchy /// /// Nicko Cadell /// Gert Driesen - internal class DefaultLoggerFactory : ILoggerFactory + internal sealed class DefaultLoggerFactory : ILoggerFactory { /// /// Create a new instance with the specified name. diff --git a/src/log4net/Repository/Hierarchy/Hierarchy.cs b/src/log4net/Repository/Hierarchy/Hierarchy.cs index 8159d128..8e7a9625 100644 --- a/src/log4net/Repository/Hierarchy/Hierarchy.cs +++ b/src/log4net/Repository/Hierarchy/Hierarchy.cs @@ -633,7 +633,7 @@ public void Clear() } /// - /// Return a new logger instance named as the first parameter using + /// Returns a new logger instance named as the first parameter using /// . /// /// The name of the logger to retrieve diff --git a/src/log4net/Repository/Hierarchy/Logger.cs b/src/log4net/Repository/Hierarchy/Logger.cs index b97cd643..54d04399 100644 --- a/src/log4net/Repository/Hierarchy/Logger.cs +++ b/src/log4net/Repository/Hierarchy/Logger.cs @@ -611,7 +611,6 @@ protected virtual void ForcedLog(LoggingEvent logEvent) /// /// /// - /// The parent of this logger. /// All loggers have at least one ancestor which is the root logger. /// /// @@ -620,13 +619,6 @@ protected virtual void ForcedLog(LoggingEvent logEvent) /// /// Loggers need to know what Hierarchy they are in. /// - /// - /// - /// Loggers need to know what Hierarchy they are in. - /// The hierarchy that this logger is a member of is stored - /// here. - /// - /// private Hierarchy? m_hierarchy; /// diff --git a/src/log4net/Util/LevelMappingEntry.cs b/src/log4net/Util/LevelMappingEntry.cs index 03cf160e..b46cd43e 100644 --- a/src/log4net/Util/LevelMappingEntry.cs +++ b/src/log4net/Util/LevelMappingEntry.cs @@ -22,41 +22,22 @@ namespace log4net.Util { /// - /// An entry in the - /// - /// - /// - /// This is an abstract base class for types that are stored in the + /// An abstract base class for types that are stored in the /// object. - /// - /// + /// /// Nicko Cadell public abstract class LevelMappingEntry : IOptionHandler { /// /// Default protected constructor /// - /// - /// - /// Default protected constructor - /// - /// protected LevelMappingEntry() { } /// - /// The level that is the key for this mapping + /// Gets or sets the level that is the key for this mapping. /// - /// - /// The that is the key for this mapping - /// - /// - /// - /// Get or set the that is the key for this - /// mapping subclass. - /// - /// public Level? Level { get; set; } /// diff --git a/src/log4net/Util/PatternStringConverters/LiteralPatternConverter.cs b/src/log4net/Util/PatternStringConverters/LiteralPatternConverter.cs index dd24df83..b7e2c73e 100644 --- a/src/log4net/Util/PatternStringConverters/LiteralPatternConverter.cs +++ b/src/log4net/Util/PatternStringConverters/LiteralPatternConverter.cs @@ -56,7 +56,7 @@ public override PatternConverter SetNext(PatternConverter pc) if (pc is LiteralPatternConverter literalPc) { // Combine the two adjacent literals together - Option = Option + literalPc.Option; + Option += literalPc.Option; // We are the next converter now return this; } diff --git a/src/log4net/Util/PatternStringConverters/UtcDatePatternConverter.cs b/src/log4net/Util/PatternStringConverters/UtcDatePatternConverter.cs index 70a392f2..408e18e4 100644 --- a/src/log4net/Util/PatternStringConverters/UtcDatePatternConverter.cs +++ b/src/log4net/Util/PatternStringConverters/UtcDatePatternConverter.cs @@ -38,7 +38,7 @@ namespace log4net.Util.PatternStringConverters /// /// /// Nicko Cadell - internal class UtcDatePatternConverter : DatePatternConverter + internal sealed class UtcDatePatternConverter : DatePatternConverter { /// /// Write the current date and time to the output diff --git a/src/log4net/Util/TypeConverters/BooleanConverter.cs b/src/log4net/Util/TypeConverters/BooleanConverter.cs index 5ba0dc99..b99c12ac 100644 --- a/src/log4net/Util/TypeConverters/BooleanConverter.cs +++ b/src/log4net/Util/TypeConverters/BooleanConverter.cs @@ -34,7 +34,7 @@ namespace log4net.Util.TypeConverters /// /// Nicko Cadell /// Gert Driesen - internal class BooleanConverter : IConvertFrom + internal sealed class BooleanConverter : IConvertFrom { /// /// Can the source type be converted to the type supported by this object diff --git a/src/log4net/Util/TypeConverters/EncodingConverter.cs b/src/log4net/Util/TypeConverters/EncodingConverter.cs index 03161f93..63c1fd01 100644 --- a/src/log4net/Util/TypeConverters/EncodingConverter.cs +++ b/src/log4net/Util/TypeConverters/EncodingConverter.cs @@ -30,7 +30,7 @@ namespace log4net.Util.TypeConverters /// /// Nicko Cadell /// Gert Driesen - internal class EncodingConverter : IConvertFrom + internal sealed class EncodingConverter : IConvertFrom { /// /// Can the source type be converted to the type supported by this object diff --git a/src/log4net/Util/TypeConverters/IPAddressConverter.cs b/src/log4net/Util/TypeConverters/IPAddressConverter.cs index fa340689..08bdc951 100644 --- a/src/log4net/Util/TypeConverters/IPAddressConverter.cs +++ b/src/log4net/Util/TypeConverters/IPAddressConverter.cs @@ -34,7 +34,7 @@ namespace log4net.Util.TypeConverters /// /// /// Nicko Cadell - internal class IPAddressConverter : IConvertFrom + internal sealed class IPAddressConverter : IConvertFrom { /// /// Can the source type be converted to the type supported by this object diff --git a/src/log4net/Util/TypeConverters/PatternLayoutConverter.cs b/src/log4net/Util/TypeConverters/PatternLayoutConverter.cs index bbd940db..dcd68509 100644 --- a/src/log4net/Util/TypeConverters/PatternLayoutConverter.cs +++ b/src/log4net/Util/TypeConverters/PatternLayoutConverter.cs @@ -36,7 +36,7 @@ namespace log4net.Util.TypeConverters /// /// /// Nicko Cadell - internal class PatternLayoutConverter : IConvertFrom + internal sealed class PatternLayoutConverter : IConvertFrom { /// /// Can the source type be converted to the type supported by this object diff --git a/src/log4net/Util/TypeConverters/PatternStringConverter.cs b/src/log4net/Util/TypeConverters/PatternStringConverter.cs index 10029874..2270008f 100644 --- a/src/log4net/Util/TypeConverters/PatternStringConverter.cs +++ b/src/log4net/Util/TypeConverters/PatternStringConverter.cs @@ -38,7 +38,7 @@ namespace log4net.Util.TypeConverters /// /// /// Nicko Cadell - internal class PatternStringConverter : IConvertTo, IConvertFrom + internal sealed class PatternStringConverter : IConvertTo, IConvertFrom { /// /// Can the target type be converted to the type supported by this object diff --git a/src/log4net/Util/TypeConverters/TypeConverter.cs b/src/log4net/Util/TypeConverters/TypeConverter.cs index 3e791e24..a36869c5 100644 --- a/src/log4net/Util/TypeConverters/TypeConverter.cs +++ b/src/log4net/Util/TypeConverters/TypeConverter.cs @@ -28,7 +28,7 @@ namespace log4net.Util.TypeConverters /// /// /// Nicko Cadell - internal class TypeConverter : IConvertFrom + internal sealed class TypeConverter : IConvertFrom { /// /// Can the source type be converted to the type supported by this object From f5806d7e708529a3225172e7877974f4fe1ec34b Mon Sep 17 00:00:00 2001 From: Erik Mavrinac Date: Wed, 27 Mar 2024 14:55:26 -0700 Subject: [PATCH 3/3] inerpolate string --- src/log4net/Repository/Hierarchy/Logger.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/log4net/Repository/Hierarchy/Logger.cs b/src/log4net/Repository/Hierarchy/Logger.cs index 54d04399..c946813d 100644 --- a/src/log4net/Repository/Hierarchy/Logger.cs +++ b/src/log4net/Repository/Hierarchy/Logger.cs @@ -496,7 +496,7 @@ protected virtual void CallAppenders(LoggingEvent loggingEvent) if (m_hierarchy is not null && !m_hierarchy.EmittedNoAppenderWarning && writes == 0) { m_hierarchy.EmittedNoAppenderWarning = true; - LogLog.Debug(declaringType, "No appenders could be found for logger [" + Name + "] repository [" + Repository?.Name + "]"); + LogLog.Debug(declaringType, $"No appenders could be found for logger [{Name}] repository [{Repository?.Name}]"); LogLog.Debug(declaringType, "Please initialize the log4net system properly."); try {