Skip to content

Commit

Permalink
Nullable and modernization part 6 (#132)
Browse files Browse the repository at this point in the history
* Expand nullability to 'object message' for logger, loggerextensions. More 'is' conversions.

* Seal some classes, reduce doc redundancy, reduce some nullables

* inerpolate string
  • Loading branch information
erikmav committed Mar 28, 2024
1 parent 5eb038a commit bb86fea
Show file tree
Hide file tree
Showing 54 changed files with 260 additions and 280 deletions.
1 change: 1 addition & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
<UseSharedCompilation>true</UseSharedCompilation>
<LangVersion>latest</LangVersion>
<Nullable>Enable</Nullable>
<WarningsAsErrors>nullable</WarningsAsErrors>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/log4net.Tests/Appender/DebugAppenderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down
2 changes: 1 addition & 1 deletion src/log4net.Tests/Appender/RollingFileAppenderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1441,7 +1441,7 @@ public void TestInitializeCountUpInfinite3()
appender.Encoding = Encoding.ASCII;
appender.ErrorHandler = handler;
appender.MaxSizeRollBackups = maxSizeRollBackups;
if (lockModel != null)
if (lockModel is not null)
{
appender.LockingModel = lockModel;
}
Expand Down
2 changes: 1 addition & 1 deletion src/log4net.Tests/Appender/SmtpPickupDirAppenderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion src/log4net.Tests/Core/DefaultRepositorySelectorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,5 @@ public IAppender[] GetAppenders()
}
}

internal class MockLoggerRepository2 : MockLoggerRepository;
internal sealed class MockLoggerRepository2 : MockLoggerRepository;
}
4 changes: 2 additions & 2 deletions src/log4net.Tests/Core/FixingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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";
}
Expand Down
110 changes: 99 additions & 11 deletions src/log4net.Tests/Core/StringFormatTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ override protected void FormatDateWithoutMillis(DateTime dateToFormat, StringBui
}
}

internal class FormatterTwo : FormatterOne
internal sealed class FormatterTwo : FormatterOne
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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!)
{
Expand Down
4 changes: 2 additions & 2 deletions src/log4net.Tests/Layout/PatternLayoutTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public void NamedPatternConverterWithPrecision2ShouldStripLessLeadingStuffIfPres
/// <summary>
/// Converter to include event message
/// </summary>
private class TestMessagePatternConverter : PatternLayoutConverter
private sealed class TestMessagePatternConverter : PatternLayoutConverter
{
/// <summary>
/// Convert the pattern to the rendered message
Expand Down Expand Up @@ -344,7 +344,7 @@ public void TestExceptionPattern()
stringAppender.Reset();
}

private class MessageAsNamePatternConverter : NamedPatternConverter
private sealed class MessageAsNamePatternConverter : NamedPatternConverter
{
protected override string GetFullyQualifiedName(LoggingEvent loggingEvent)
{
Expand Down
4 changes: 2 additions & 2 deletions src/log4net.Tests/Util/PatternStringTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
22 changes: 11 additions & 11 deletions src/log4net/Appender/AdoNetAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,15 +376,15 @@ protected override void OnClose()
/// </remarks>
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)
{
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -550,7 +550,7 @@ protected virtual void Prepare(IDbCommand dbCmd)
/// </returns>
protected virtual string GetLogStatement(LoggingEvent logEvent)
{
if (Layout == null)
if (Layout is null)
{
ErrorHandler.Error("AdoNetAppender: No Layout specified.");
return "";
Expand Down Expand Up @@ -587,7 +587,7 @@ protected virtual IDbConnection CreateConnection(Type connectionType, string con
/// <returns>A connection string used to connect to the database.</returns>
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;
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -694,7 +694,7 @@ private void InitializeDatabaseConnection()
/// </remarks>
private void DisposeConnection()
{
if (Connection != null)
if (Connection is not null)
{
try
{
Expand Down
2 changes: 1 addition & 1 deletion src/log4net/Appender/AppenderSkeleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down

0 comments on commit bb86fea

Please sign in to comment.