Skip to content

Commit c4df728

Browse files
authored
Merge pull request #141 from apache/Feature/124-nullable-net8
#124 fixed more nullable Warnings when targeting net8.0
2 parents 95383f3 + a4d877f commit c4df728

File tree

59 files changed

+1308
-994
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1308
-994
lines changed

.editorconfig

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,15 @@ csharp_style_prefer_not_pattern = true:suggestion
132132
csharp_style_prefer_extended_property_pattern = true:suggestion
133133
csharp_style_var_for_built_in_types = false:silent
134134
csharp_style_var_when_type_is_apparent = false:silent
135-
csharp_style_var_elsewhere = false:silent
135+
csharp_style_var_elsewhere = false:silent
136+
137+
# SYSLIB0003: Type or member is obsolete
138+
dotnet_diagnostic.SYSLIB0003.severity = none
139+
# SYSLIB0005: Type or member is obsolete
140+
dotnet_diagnostic.SYSLIB0005.severity = none
141+
# SYSLIB0014: Type or member is obsolete
142+
dotnet_diagnostic.SYSLIB0014.severity = none
143+
# SYSLIB0050: Type or member is obsolete
144+
dotnet_diagnostic.SYSLIB0050.severity = none
145+
# SYSLIB0051: Type or member is obsolete
146+
dotnet_diagnostic.SYSLIB0051.severity = none

src/integration-testing/log4net2-SerializeEvent/SerializeEventProgram.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Diagnostics;
3+
using System.Globalization;
34
using System.IO;
45
using System.Runtime.Serialization.Formatters.Binary;
56
using log4net.Core;
@@ -13,7 +14,7 @@
1314
// If for some reason a new serialized file is needed, run this program and
1415
// commit the result over the cached version.
1516

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

1819
var stackTrace = new StackTrace(true);
1920

Binary file not shown.

src/log4net.Tests/Core/LoggingEventTest.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using log4net.Util;
2525
using NUnit.Framework;
2626
using System;
27+
using System.Globalization;
2728
using System.IO;
2829
using System.Runtime.Serialization.Formatters.Binary;
2930

@@ -32,10 +33,12 @@ namespace log4net.Tests.Core
3233
[TestFixture]
3334
public class LoggingEventTest
3435
{
36+
DateTime localTime = new DateTime(2000, 7, 1, 0, 0, 0, 0, CultureInfo.InvariantCulture.Calendar, DateTimeKind.Local);
37+
3538
[Test]
3639
public void SerializeDeserialize_BinaryFormatter()
3740
{
38-
var timestamp = new DateTime(2000, 7, 1).ToUniversalTime();
41+
var timestamp = localTime.ToUniversalTime();
3942
var ev = new LoggingEvent(new LoggingEventData
4043
{
4144
LoggerName = "aLogger",
@@ -106,8 +109,8 @@ public void DeserializeV2()
106109
Assert.AreEqual("aDomain", ev.Domain);
107110
Assert.AreEqual(1, ev.Properties.Count);
108111
Assert.AreEqual("bar", ev.Properties["foo"]);
109-
Assert.AreEqual(new DateTime(2000, 7, 1), ev.TimeStampUtc);
112+
Assert.AreEqual(localTime.ToUniversalTime(), ev.TimeStampUtc);
110113
}
111114
}
112115
}
113-
#endif // NET462_OR_GREATER
116+
#endif // NET462_OR_GREATER

src/log4net/Appender/AdoNetAppender.cs

Lines changed: 60 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -390,30 +390,28 @@ protected override void SendBuffer(LoggingEvent[] events)
390390
{
391391
// Create transaction
392392
// NJC - Do this on 2 lines because it can confuse the debugger
393-
using (IDbTransaction dbTran = Connection.BeginTransaction())
393+
using IDbTransaction dbTran = Connection.BeginTransaction();
394+
try
395+
{
396+
SendBuffer(dbTran, events);
397+
398+
// commit transaction
399+
dbTran.Commit();
400+
}
401+
catch (Exception ex)
394402
{
403+
// rollback the transaction
395404
try
396405
{
397-
SendBuffer(dbTran, events);
398-
399-
// commit transaction
400-
dbTran.Commit();
406+
dbTran.Rollback();
401407
}
402-
catch (Exception ex)
408+
catch (Exception)
403409
{
404-
// rollback the transaction
405-
try
406-
{
407-
dbTran.Rollback();
408-
}
409-
catch (Exception)
410-
{
411-
// Ignore exception
412-
}
413-
414-
// Can't insert into the database. That's a bad thing
415-
ErrorHandler.Error("Exception while writing to database", ex);
410+
// Ignore exception
416411
}
412+
413+
// Can't insert into the database. That's a bad thing
414+
ErrorHandler.Error("Exception while writing to database", ex);
417415
}
418416
}
419417
else
@@ -454,70 +452,60 @@ protected virtual void SendBuffer(IDbTransaction? dbTran, LoggingEvent[] events)
454452
{
455453
if (!string.IsNullOrWhiteSpace(CommandText))
456454
{
457-
using (IDbCommand dbCmd = Connection!.CreateCommand())
455+
using IDbCommand dbCmd = Connection.EnsureNotNull().CreateCommand();
456+
dbCmd.CommandText = CommandText;
457+
dbCmd.CommandType = CommandType;
458+
if (dbTran is not null)
459+
{
460+
dbCmd.Transaction = dbTran;
461+
}
462+
try
463+
{
464+
// prepare the command, which is significantly faster
465+
Prepare(dbCmd);
466+
}
467+
catch (Exception)
458468
{
459-
// Set the command string
460-
dbCmd.CommandText = CommandText;
461-
462-
// Set the command type
463-
dbCmd.CommandType = CommandType;
464-
// Send buffer using the prepared command object
465469
if (dbTran is not null)
466470
{
467-
dbCmd.Transaction = dbTran;
471+
// rethrow exception in transaction mode, cuz now transaction is in failed state
472+
throw;
468473
}
474+
// ignore prepare exceptions as they can happen without affecting actual logging, eg on npgsql
475+
}
469476

470-
try
471-
{
472-
// prepare the command, which is significantly faster
473-
Prepare(dbCmd);
474-
}
475-
catch (Exception)
477+
// run for all events
478+
foreach (LoggingEvent e in events)
479+
{
480+
// No need to clear dbCmd.Parameters, just use existing.
481+
// Set the parameter values
482+
foreach (AdoNetAppenderParameter param in m_parameters)
476483
{
477-
if (dbTran is not null)
478-
{
479-
// rethrow exception in transaction mode, cuz now transaction is in failed state
480-
throw;
481-
}
482-
483-
// ignore prepare exceptions as they can happen without affecting actual logging, eg on npgsql
484+
param.FormatValue(dbCmd, e);
484485
}
485486

486-
// run for all events
487-
foreach (LoggingEvent e in events)
488-
{
489-
// No need to clear dbCmd.Parameters, just use existing.
490-
// Set the parameter values
491-
foreach (AdoNetAppenderParameter param in m_parameters)
492-
{
493-
param.FormatValue(dbCmd, e);
494-
}
495-
496-
// Execute the query
497-
dbCmd.ExecuteNonQuery();
498-
}
487+
// Execute the query
488+
dbCmd.ExecuteNonQuery();
499489
}
500490
}
501491
else
502492
{
503493
// create a new command
504-
using (IDbCommand dbCmd = Connection!.CreateCommand())
494+
using IDbCommand dbCmd = Connection!.CreateCommand();
495+
if (dbTran is not null)
505496
{
506-
if (dbTran is not null)
507-
{
508-
dbCmd.Transaction = dbTran;
509-
}
510-
// run for all events
511-
foreach (LoggingEvent e in events)
512-
{
513-
// Get the command text from the Layout
514-
string logStatement = GetLogStatement(e);
497+
dbCmd.Transaction = dbTran;
498+
}
499+
// run for all events
500+
foreach (LoggingEvent e in events)
501+
{
502+
// Get the command text from the Layout
503+
string logStatement = GetLogStatement(e);
515504

516-
LogLog.Debug(declaringType, $"LogStatement [{logStatement}]");
505+
LogLog.Debug(declaringType, $"LogStatement [{logStatement}]");
517506

518-
dbCmd.CommandText = logStatement;
519-
dbCmd.ExecuteNonQuery();
520-
}
507+
dbCmd.CommandText = logStatement;
508+
dbCmd.ExecuteNonQuery();
521509
}
522510
}
523511
}
@@ -557,7 +545,7 @@ protected virtual string GetLogStatement(LoggingEvent logEvent)
557545
}
558546
else
559547
{
560-
using StringWriter writer = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);
548+
using StringWriter writer = new(System.Globalization.CultureInfo.InvariantCulture);
561549
Layout.Format(writer, logEvent);
562550
return writer.ToString();
563551
}
@@ -574,7 +562,7 @@ protected virtual string GetLogStatement(LoggingEvent logEvent)
574562
/// <returns>An <see cref="IDbConnection"/> instance with a valid connection string.</returns>
575563
protected virtual IDbConnection CreateConnection(Type connectionType, string connectionString)
576564
{
577-
IDbConnection connection = (IDbConnection)Activator.CreateInstance(connectionType);
565+
IDbConnection connection = Activator.CreateInstance(connectionType).EnsureIs<IDbConnection>();
578566
connection.ConnectionString = connectionString;
579567
return connection;
580568
}
@@ -587,7 +575,7 @@ protected virtual IDbConnection CreateConnection(Type connectionType, string con
587575
/// <returns>A connection string used to connect to the database.</returns>
588576
protected virtual string ResolveConnectionString(out string connectionStringContext)
589577
{
590-
if (ConnectionString is not null && ConnectionString.Length > 0)
578+
if (ConnectionString is { Length: > 0 })
591579
{
592580
connectionStringContext = "ConnectionString";
593581
return ConnectionString;
@@ -607,7 +595,7 @@ protected virtual string ResolveConnectionString(out string connectionStringCont
607595
}
608596
}
609597

610-
if (AppSettingsKey is not null && AppSettingsKey.Length > 0)
598+
if (AppSettingsKey is { Length: > 0 })
611599
{
612600
connectionStringContext = "AppSettingsKey";
613601
string? appSettingsConnectionString = SystemInfo.GetAppSetting(AppSettingsKey);
@@ -643,7 +631,7 @@ protected virtual Type ResolveConnectionType()
643631
{
644632
if (SystemInfo.GetTypeFromString(ConnectionType, true, false) is Type t)
645633
{
646-
return t;
634+
return t;
647635
}
648636
throw new InvalidOperationException($"Connection type {ConnectionType} was not found in any assembly");
649637
}
@@ -931,7 +919,7 @@ public virtual void Prepare(IDbCommand command)
931919
public virtual void FormatValue(IDbCommand command, LoggingEvent loggingEvent)
932920
{
933921
// Lookup the parameter
934-
IDbDataParameter param = (IDbDataParameter)command.Parameters[ParameterName];
922+
IDbDataParameter param = (IDbDataParameter)command.Parameters[ParameterName.EnsureNotNull()];
935923

936924
// Format the value
937925
object? formattedValue = Layout?.Format(loggingEvent);

src/log4net/Appender/AppenderCollection.cs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,7 @@ public interface IAppenderCollectionEnumerator : IEnumerator<IAppender>
5151
/// An <c>AppenderCollection</c> wrapper that is read-only.
5252
/// </returns>
5353
public static AppenderCollection ReadOnly(AppenderCollection list)
54-
{
55-
if (list is null)
56-
{
57-
throw new ArgumentNullException(nameof(list));
58-
}
59-
60-
return new ReadOnlyAppenderCollection(list);
61-
}
54+
=> new ReadOnlyAppenderCollection(list.EnsureNotNull());
6255

6356
/// <summary>
6457
/// An empty readonly static AppenderCollection
@@ -69,10 +62,7 @@ public static AppenderCollection ReadOnly(AppenderCollection list)
6962
/// Initializes a new instance of the <c>AppenderCollection</c> class
7063
/// that is empty and has the default initial capacity.
7164
/// </summary>
72-
public AppenderCollection()
73-
{
74-
m_array = new IAppender[DEFAULT_CAPACITY];
75-
}
65+
public AppenderCollection() => m_array = new IAppender[DEFAULT_CAPACITY];
7666

7767
/// <summary>
7868
/// Initializes a new instance of the <c>AppenderCollection</c> class
@@ -81,10 +71,7 @@ public AppenderCollection()
8171
/// <param name="capacity">
8272
/// The number of elements that the new <c>AppenderCollection</c> is initially capable of storing.
8373
/// </param>
84-
public AppenderCollection(int capacity)
85-
{
86-
m_array = new IAppender[capacity];
87-
}
74+
public AppenderCollection(int capacity) => m_array = new IAppender[capacity];
8875

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

533-
object IList.this[int i]
520+
object? IList.this[int i]
534521
{
535522
get => this[i];
536-
set => this[i] = (IAppender)value;
523+
set
524+
{
525+
this[i] = value.EnsureIs<IAppender>();
526+
}
537527
}
538528

539529
int IList.Add(object? x)
@@ -556,11 +546,11 @@ bool IList.Contains(object? x)
556546
return false;
557547
}
558548

559-
int IList.IndexOf(object x) => IndexOf((IAppender)x);
549+
int IList.IndexOf(object? x) => IndexOf(x.EnsureIs<IAppender>());
560550

561-
void IList.Insert(int pos, object x) => Insert(pos, (IAppender)x);
551+
void IList.Insert(int pos, object? x) => Insert(pos, x.EnsureIs<IAppender>());
562552

563-
void IList.Remove(object x) => Remove((IAppender)x);
553+
void IList.Remove(object? x) => Remove(x.EnsureIs<IAppender>());
564554

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

0 commit comments

Comments
 (0)