Skip to content

Commit f0b5ad4

Browse files
committed
#124 fixed more nullable Warnings when targeting net8.0
1 parent babde01 commit f0b5ad4

36 files changed

+463
-953
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/log4net/Appender/AdoNetAppender.cs

Lines changed: 65 additions & 71 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,66 @@ 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!.CreateCommand();
456+
// Set the command string
457+
dbCmd.CommandText = CommandText;
458+
459+
// Set the command type
460+
dbCmd.CommandType = CommandType;
461+
// Send buffer using the prepared command object
462+
if (dbTran is not null)
458463
{
459-
// Set the command string
460-
dbCmd.CommandText = CommandText;
464+
dbCmd.Transaction = dbTran;
465+
}
461466

462-
// Set the command type
463-
dbCmd.CommandType = CommandType;
464-
// Send buffer using the prepared command object
467+
try
468+
{
469+
// prepare the command, which is significantly faster
470+
Prepare(dbCmd);
471+
}
472+
catch (Exception)
473+
{
465474
if (dbTran is not null)
466475
{
467-
dbCmd.Transaction = dbTran;
468-
}
469-
470-
try
471-
{
472-
// prepare the command, which is significantly faster
473-
Prepare(dbCmd);
476+
// rethrow exception in transaction mode, cuz now transaction is in failed state
477+
throw;
474478
}
475-
catch (Exception)
476-
{
477-
if (dbTran is not null)
478-
{
479-
// rethrow exception in transaction mode, cuz now transaction is in failed state
480-
throw;
481-
}
482479

483-
// ignore prepare exceptions as they can happen without affecting actual logging, eg on npgsql
484-
}
480+
// ignore prepare exceptions as they can happen without affecting actual logging, eg on npgsql
481+
}
485482

486-
// run for all events
487-
foreach (LoggingEvent e in events)
483+
// run for all events
484+
foreach (LoggingEvent e in events)
485+
{
486+
// No need to clear dbCmd.Parameters, just use existing.
487+
// Set the parameter values
488+
foreach (AdoNetAppenderParameter param in m_parameters)
488489
{
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();
490+
param.FormatValue(dbCmd, e);
498491
}
492+
493+
// Execute the query
494+
dbCmd.ExecuteNonQuery();
499495
}
500496
}
501497
else
502498
{
503499
// create a new command
504-
using (IDbCommand dbCmd = Connection!.CreateCommand())
500+
using IDbCommand dbCmd = Connection!.CreateCommand();
501+
if (dbTran is not null)
505502
{
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);
503+
dbCmd.Transaction = dbTran;
504+
}
505+
// run for all events
506+
foreach (LoggingEvent e in events)
507+
{
508+
// Get the command text from the Layout
509+
string logStatement = GetLogStatement(e);
515510

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

518-
dbCmd.CommandText = logStatement;
519-
dbCmd.ExecuteNonQuery();
520-
}
513+
dbCmd.CommandText = logStatement;
514+
dbCmd.ExecuteNonQuery();
521515
}
522516
}
523517
}
@@ -557,7 +551,7 @@ protected virtual string GetLogStatement(LoggingEvent logEvent)
557551
}
558552
else
559553
{
560-
using StringWriter writer = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);
554+
using StringWriter writer = new(System.Globalization.CultureInfo.InvariantCulture);
561555
Layout.Format(writer, logEvent);
562556
return writer.ToString();
563557
}
@@ -574,7 +568,7 @@ protected virtual string GetLogStatement(LoggingEvent logEvent)
574568
/// <returns>An <see cref="IDbConnection"/> instance with a valid connection string.</returns>
575569
protected virtual IDbConnection CreateConnection(Type connectionType, string connectionString)
576570
{
577-
IDbConnection connection = (IDbConnection)Activator.CreateInstance(connectionType);
571+
IDbConnection connection = Activator.CreateInstance(connectionType).EnsureIs<IDbConnection>();
578572
connection.ConnectionString = connectionString;
579573
return connection;
580574
}
@@ -587,7 +581,7 @@ protected virtual IDbConnection CreateConnection(Type connectionType, string con
587581
/// <returns>A connection string used to connect to the database.</returns>
588582
protected virtual string ResolveConnectionString(out string connectionStringContext)
589583
{
590-
if (ConnectionString is not null && ConnectionString.Length > 0)
584+
if (ConnectionString is string { Length: > 0 })
591585
{
592586
connectionStringContext = "ConnectionString";
593587
return ConnectionString;
@@ -607,7 +601,7 @@ protected virtual string ResolveConnectionString(out string connectionStringCont
607601
}
608602
}
609603

610-
if (AppSettingsKey is not null && AppSettingsKey.Length > 0)
604+
if (AppSettingsKey is string { Length: > 0 })
611605
{
612606
connectionStringContext = "AppSettingsKey";
613607
string? appSettingsConnectionString = SystemInfo.GetAppSetting(AppSettingsKey);
@@ -643,7 +637,7 @@ protected virtual Type ResolveConnectionType()
643637
{
644638
if (SystemInfo.GetTypeFromString(ConnectionType, true, false) is Type t)
645639
{
646-
return t;
640+
return t;
647641
}
648642
throw new InvalidOperationException($"Connection type {ConnectionType} was not found in any assembly");
649643
}
@@ -931,7 +925,7 @@ public virtual void Prepare(IDbCommand command)
931925
public virtual void FormatValue(IDbCommand command, LoggingEvent loggingEvent)
932926
{
933927
// Lookup the parameter
934-
IDbDataParameter param = (IDbDataParameter)command.Parameters[ParameterName];
928+
IDbDataParameter param = (IDbDataParameter)command.Parameters[ParameterName.EnsureNotNull()];
935929

936930
// Format the value
937931
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

src/log4net/Appender/FileAppender.cs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ namespace log4net.Appender
6262
/// <para>
6363
/// The <see cref="FileAppender"/> supports pluggable file locking models via
6464
/// the <see cref="LockingModel"/> property.
65-
/// The default behavior, implemented by <see cref="FileAppender.ExclusiveLock"/>
65+
/// The default behavior, implemented by <see cref="ExclusiveLock"/>
6666
/// is to obtain an exclusive write lock on the file until this appender is closed.
6767
/// The alternative models only hold a
68-
/// write lock while the appender is writing a logging event (<see cref="FileAppender.MinimalLock"/>)
69-
/// or synchronize by using a named system-wide Mutex (<see cref="FileAppender.InterProcessLock"/>).
68+
/// write lock while the appender is writing a logging event (<see cref="MinimalLock"/>)
69+
/// or synchronize by using a named system-wide Mutex (<see cref="InterProcessLock"/>).
7070
/// </para>
7171
/// <para>
7272
/// All locking strategies have issues and you should seriously consider using a different strategy that
@@ -111,15 +111,7 @@ private LockStateException(SerializationInfo info, StreamingContext context) : b
111111
private readonly LockingModelBase m_lockingModel;
112112
private int m_lockLevel;
113113

114-
public LockingStream(LockingModelBase locking)
115-
{
116-
if (locking is null)
117-
{
118-
throw new ArgumentNullException(nameof(locking));
119-
}
120-
121-
m_lockingModel = locking;
122-
}
114+
public LockingStream(LockingModelBase locking) => m_lockingModel = locking.EnsureNotNull();
123115

124116
protected override void Dispose(bool disposing)
125117
{
@@ -479,7 +471,7 @@ public override void CloseFile()
479471
/// </remarks>
480472
public override Stream? AcquireLock()
481473
{
482-
return m_stream;
474+
return m_stream;
483475
}
484476

485477
/// <summary>
@@ -519,7 +511,7 @@ public override void OnClose()
519511
/// <para>
520512
/// Opens the file once for each <see cref="AcquireLock"/>/<see cref="ReleaseLock"/> cycle,
521513
/// thus holding the lock for the minimal amount of time. This method of locking
522-
/// is considerably slower than <see cref="FileAppender.ExclusiveLock"/> but allows
514+
/// is considerably slower than <see cref="ExclusiveLock"/> but allows
523515
/// other processes to move/delete the log file whilst logging continues.
524516
/// </para>
525517
/// </remarks>
@@ -963,7 +955,7 @@ public virtual string? File
963955
/// </value>
964956
/// <remarks>
965957
/// <para>
966-
/// The default encoding set is <see cref="System.Text.Encoding.Default"/>
958+
/// The default encoding set is <see cref="Encoding.Default"/>
967959
/// which is the encoding for the system's current ANSI code page.
968960
/// </para>
969961
/// </remarks>
@@ -986,26 +978,27 @@ public virtual string? File
986978
public SecurityContext? SecurityContext { get; set; }
987979

988980
/// <summary>
989-
/// Gets or sets the <see cref="FileAppender.LockingModel"/> used to handle locking of the file.
981+
/// Gets or sets the <see cref="LockingModel"/> used to handle locking of the file.
990982
/// </summary>
991983
/// <value>
992-
/// The <see cref="FileAppender.LockingModel"/> used to lock the file.
984+
/// The <see cref="LockingModel"/> used to lock the file.
993985
/// </value>
994986
/// <remarks>
995987
/// <para>
996-
/// Gets or sets the <see cref="FileAppender.LockingModel"/> used to handle locking of the file.
988+
/// Gets or sets the <see cref="LockingModel"/> used to handle locking of the file.
997989
/// </para>
998990
/// <para>
999-
/// There are three built in locking models, <see cref="FileAppender.ExclusiveLock"/>, <see cref="FileAppender.MinimalLock"/> and <see cref="FileAppender.InterProcessLock"/> .
991+
/// There are three built in locking models, <see cref="ExclusiveLock"/>, <see cref="MinimalLock"/> and <see cref="InterProcessLock"/> .
1000992
/// The first locks the file from the start of logging to the end, the
1001993
/// second locks only for the minimal amount of time when logging each message
1002994
/// and the last synchronizes processes using a named system-wide Mutex.
1003995
/// </para>
1004996
/// <para>
1005-
/// The default locking model is the <see cref="FileAppender.ExclusiveLock"/>.
997+
/// The default locking model is the <see cref="ExclusiveLock"/>.
1006998
/// </para>
1007999
/// </remarks>
1008-
public LockingModelBase LockingModel { get; set; } = (LockingModelBase)Activator.CreateInstance(defaultLockingModelType);
1000+
public LockingModelBase LockingModel { get; set; }
1001+
= Activator.CreateInstance(defaultLockingModelType).EnsureIs<LockingModelBase>();
10091002

10101003
/// <summary>
10111004
/// Activate the options on the file appender.

0 commit comments

Comments
 (0)