Skip to content

Commit

Permalink
Merge pull request #141 from apache/Feature/124-nullable-net8
Browse files Browse the repository at this point in the history
#124 fixed more nullable Warnings when targeting net8.0
  • Loading branch information
FreeAndNil committed Apr 8, 2024
2 parents 95383f3 + a4d877f commit c4df728
Show file tree
Hide file tree
Showing 59 changed files with 1,308 additions and 994 deletions.
13 changes: 12 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,15 @@ csharp_style_prefer_not_pattern = true:suggestion
csharp_style_prefer_extended_property_pattern = true:suggestion
csharp_style_var_for_built_in_types = false:silent
csharp_style_var_when_type_is_apparent = false:silent
csharp_style_var_elsewhere = false:silent
csharp_style_var_elsewhere = false:silent

# SYSLIB0003: Type or member is obsolete
dotnet_diagnostic.SYSLIB0003.severity = none
# SYSLIB0005: Type or member is obsolete
dotnet_diagnostic.SYSLIB0005.severity = none
# SYSLIB0014: Type or member is obsolete
dotnet_diagnostic.SYSLIB0014.severity = none
# SYSLIB0050: Type or member is obsolete
dotnet_diagnostic.SYSLIB0050.severity = none
# SYSLIB0051: Type or member is obsolete
dotnet_diagnostic.SYSLIB0051.severity = none
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using log4net.Core;
Expand All @@ -13,7 +14,7 @@
// If for some reason a new serialized file is needed, run this program and
// commit the result over the cached version.

var localTimestamp = new DateTime(2000, 7, 1).ToLocalTime();
var localTimestamp = new DateTime(2000, 7, 1, 0, 0, 0, 0, CultureInfo.InvariantCulture.Calendar, DateTimeKind.Local);

var stackTrace = new StackTrace(true);

Expand Down
Binary file not shown.
9 changes: 6 additions & 3 deletions src/log4net.Tests/Core/LoggingEventTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using log4net.Util;
using NUnit.Framework;
using System;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

Expand All @@ -32,10 +33,12 @@ namespace log4net.Tests.Core
[TestFixture]
public class LoggingEventTest
{
DateTime localTime = new DateTime(2000, 7, 1, 0, 0, 0, 0, CultureInfo.InvariantCulture.Calendar, DateTimeKind.Local);

[Test]
public void SerializeDeserialize_BinaryFormatter()
{
var timestamp = new DateTime(2000, 7, 1).ToUniversalTime();
var timestamp = localTime.ToUniversalTime();
var ev = new LoggingEvent(new LoggingEventData
{
LoggerName = "aLogger",
Expand Down Expand Up @@ -106,8 +109,8 @@ public void DeserializeV2()
Assert.AreEqual("aDomain", ev.Domain);
Assert.AreEqual(1, ev.Properties.Count);
Assert.AreEqual("bar", ev.Properties["foo"]);
Assert.AreEqual(new DateTime(2000, 7, 1), ev.TimeStampUtc);
Assert.AreEqual(localTime.ToUniversalTime(), ev.TimeStampUtc);
}
}
}
#endif // NET462_OR_GREATER
#endif // NET462_OR_GREATER
132 changes: 60 additions & 72 deletions src/log4net/Appender/AdoNetAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -390,30 +390,28 @@ protected override void SendBuffer(LoggingEvent[] events)
{
// Create transaction
// NJC - Do this on 2 lines because it can confuse the debugger
using (IDbTransaction dbTran = Connection.BeginTransaction())
using IDbTransaction dbTran = Connection.BeginTransaction();
try
{
SendBuffer(dbTran, events);

// commit transaction
dbTran.Commit();
}
catch (Exception ex)
{
// rollback the transaction
try
{
SendBuffer(dbTran, events);

// commit transaction
dbTran.Commit();
dbTran.Rollback();
}
catch (Exception ex)
catch (Exception)
{
// rollback the transaction
try
{
dbTran.Rollback();
}
catch (Exception)
{
// Ignore exception
}

// Can't insert into the database. That's a bad thing
ErrorHandler.Error("Exception while writing to database", ex);
// Ignore exception
}

// Can't insert into the database. That's a bad thing
ErrorHandler.Error("Exception while writing to database", ex);
}
}
else
Expand Down Expand Up @@ -454,70 +452,60 @@ protected virtual void SendBuffer(IDbTransaction? dbTran, LoggingEvent[] events)
{
if (!string.IsNullOrWhiteSpace(CommandText))
{
using (IDbCommand dbCmd = Connection!.CreateCommand())
using IDbCommand dbCmd = Connection.EnsureNotNull().CreateCommand();
dbCmd.CommandText = CommandText;
dbCmd.CommandType = CommandType;
if (dbTran is not null)
{
dbCmd.Transaction = dbTran;
}
try
{
// prepare the command, which is significantly faster
Prepare(dbCmd);
}
catch (Exception)
{
// Set the command string
dbCmd.CommandText = CommandText;

// Set the command type
dbCmd.CommandType = CommandType;
// Send buffer using the prepared command object
if (dbTran is not null)
{
dbCmd.Transaction = dbTran;
// rethrow exception in transaction mode, cuz now transaction is in failed state
throw;
}
// ignore prepare exceptions as they can happen without affecting actual logging, eg on npgsql
}

try
{
// prepare the command, which is significantly faster
Prepare(dbCmd);
}
catch (Exception)
// run for all events
foreach (LoggingEvent e in events)
{
// No need to clear dbCmd.Parameters, just use existing.
// Set the parameter values
foreach (AdoNetAppenderParameter param in m_parameters)
{
if (dbTran is not null)
{
// rethrow exception in transaction mode, cuz now transaction is in failed state
throw;
}

// ignore prepare exceptions as they can happen without affecting actual logging, eg on npgsql
param.FormatValue(dbCmd, e);
}

// run for all events
foreach (LoggingEvent e in events)
{
// No need to clear dbCmd.Parameters, just use existing.
// Set the parameter values
foreach (AdoNetAppenderParameter param in m_parameters)
{
param.FormatValue(dbCmd, e);
}

// Execute the query
dbCmd.ExecuteNonQuery();
}
// Execute the query
dbCmd.ExecuteNonQuery();
}
}
else
{
// create a new command
using (IDbCommand dbCmd = Connection!.CreateCommand())
using IDbCommand dbCmd = Connection!.CreateCommand();
if (dbTran is not null)
{
if (dbTran is not null)
{
dbCmd.Transaction = dbTran;
}
// run for all events
foreach (LoggingEvent e in events)
{
// Get the command text from the Layout
string logStatement = GetLogStatement(e);
dbCmd.Transaction = dbTran;
}
// run for all events
foreach (LoggingEvent e in events)
{
// Get the command text from the Layout
string logStatement = GetLogStatement(e);

LogLog.Debug(declaringType, $"LogStatement [{logStatement}]");
LogLog.Debug(declaringType, $"LogStatement [{logStatement}]");

dbCmd.CommandText = logStatement;
dbCmd.ExecuteNonQuery();
}
dbCmd.CommandText = logStatement;
dbCmd.ExecuteNonQuery();
}
}
}
Expand Down Expand Up @@ -557,7 +545,7 @@ protected virtual string GetLogStatement(LoggingEvent logEvent)
}
else
{
using StringWriter writer = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);
using StringWriter writer = new(System.Globalization.CultureInfo.InvariantCulture);
Layout.Format(writer, logEvent);
return writer.ToString();
}
Expand All @@ -574,7 +562,7 @@ protected virtual string GetLogStatement(LoggingEvent logEvent)
/// <returns>An <see cref="IDbConnection"/> instance with a valid connection string.</returns>
protected virtual IDbConnection CreateConnection(Type connectionType, string connectionString)
{
IDbConnection connection = (IDbConnection)Activator.CreateInstance(connectionType);
IDbConnection connection = Activator.CreateInstance(connectionType).EnsureIs<IDbConnection>();
connection.ConnectionString = connectionString;
return connection;
}
Expand All @@ -587,7 +575,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 is not null && ConnectionString.Length > 0)
if (ConnectionString is { Length: > 0 })
{
connectionStringContext = "ConnectionString";
return ConnectionString;
Expand All @@ -607,7 +595,7 @@ protected virtual string ResolveConnectionString(out string connectionStringCont
}
}

if (AppSettingsKey is not null && AppSettingsKey.Length > 0)
if (AppSettingsKey is { Length: > 0 })
{
connectionStringContext = "AppSettingsKey";
string? appSettingsConnectionString = SystemInfo.GetAppSetting(AppSettingsKey);
Expand Down Expand Up @@ -643,7 +631,7 @@ protected virtual Type ResolveConnectionType()
{
if (SystemInfo.GetTypeFromString(ConnectionType, true, false) is Type t)
{
return t;
return t;
}
throw new InvalidOperationException($"Connection type {ConnectionType} was not found in any assembly");
}
Expand Down Expand Up @@ -931,7 +919,7 @@ public virtual void Prepare(IDbCommand command)
public virtual void FormatValue(IDbCommand command, LoggingEvent loggingEvent)
{
// Lookup the parameter
IDbDataParameter param = (IDbDataParameter)command.Parameters[ParameterName];
IDbDataParameter param = (IDbDataParameter)command.Parameters[ParameterName.EnsureNotNull()];

// Format the value
object? formattedValue = Layout?.Format(loggingEvent);
Expand Down
32 changes: 11 additions & 21 deletions src/log4net/Appender/AppenderCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,7 @@ public interface IAppenderCollectionEnumerator : IEnumerator<IAppender>
/// An <c>AppenderCollection</c> wrapper that is read-only.
/// </returns>
public static AppenderCollection ReadOnly(AppenderCollection list)
{
if (list is null)
{
throw new ArgumentNullException(nameof(list));
}

return new ReadOnlyAppenderCollection(list);
}
=> new ReadOnlyAppenderCollection(list.EnsureNotNull());

/// <summary>
/// An empty readonly static AppenderCollection
Expand All @@ -69,10 +62,7 @@ public static AppenderCollection ReadOnly(AppenderCollection list)
/// Initializes a new instance of the <c>AppenderCollection</c> class
/// that is empty and has the default initial capacity.
/// </summary>
public AppenderCollection()
{
m_array = new IAppender[DEFAULT_CAPACITY];
}
public AppenderCollection() => m_array = new IAppender[DEFAULT_CAPACITY];

/// <summary>
/// Initializes a new instance of the <c>AppenderCollection</c> class
Expand All @@ -81,10 +71,7 @@ public AppenderCollection()
/// <param name="capacity">
/// The number of elements that the new <c>AppenderCollection</c> is initially capable of storing.
/// </param>
public AppenderCollection(int capacity)
{
m_array = new IAppender[capacity];
}
public AppenderCollection(int capacity) => m_array = new IAppender[capacity];

/// <summary>
/// Initializes a new instance of the <c>AppenderCollection</c> class
Expand Down Expand Up @@ -530,10 +517,13 @@ void ICollection.CopyTo(Array array, int start)
}
}

object IList.this[int i]
object? IList.this[int i]
{
get => this[i];
set => this[i] = (IAppender)value;
set
{
this[i] = value.EnsureIs<IAppender>();
}
}

int IList.Add(object? x)
Expand All @@ -556,11 +546,11 @@ bool IList.Contains(object? x)
return false;
}

int IList.IndexOf(object x) => IndexOf((IAppender)x);
int IList.IndexOf(object? x) => IndexOf(x.EnsureIs<IAppender>());

void IList.Insert(int pos, object x) => Insert(pos, (IAppender)x);
void IList.Insert(int pos, object? x) => Insert(pos, x.EnsureIs<IAppender>());

void IList.Remove(object x) => Remove((IAppender)x);
void IList.Remove(object? x) => Remove(x.EnsureIs<IAppender>());

void IList.RemoveAt(int pos) => RemoveAt(pos);

Expand Down
Loading

0 comments on commit c4df728

Please sign in to comment.