Skip to content

Commit 1296388

Browse files
committed
1 parent c40eb18 commit 1296388

36 files changed

+360
-392
lines changed

src/log4net.Tests/Appender/RollingFileAppenderTest.cs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -359,19 +359,19 @@ public RollFileEntry(string fileName, long fileLength)
359359
/// A table of entries showing files that should exist and their expected sizes
360360
/// after a message is logged
361361
/// </param>
362-
public sealed class RollConditions(List<RollFileEntry> preLogFileEntries, List<RollFileEntry> postLogFileEntries)
362+
public sealed class RollConditions(IList<RollFileEntry> preLogFileEntries, IList<RollFileEntry> postLogFileEntries)
363363
{
364364
/// <summary>
365365
/// A table of entries showing files that should exist and their expected sizes
366366
/// before logging is called
367367
/// </summary>
368-
public List<RollFileEntry> GetPreLogFileEntries() => preLogFileEntries;
368+
public IList<RollFileEntry> PreLogFileEntries => preLogFileEntries;
369369

370370
/// <summary>
371371
/// A table of entries showing files that should exist and their expected sizes
372372
/// after a message is logged
373373
/// </summary>
374-
public List<RollFileEntry> GetPostLogFileEntries() => postLogFileEntries;
374+
public IList<RollFileEntry> PostLogFileEntries => postLogFileEntries;
375375
}
376376

377377
private static void VerifyExistenceAndRemoveFromList(List<string> alExisting,
@@ -389,7 +389,7 @@ private static void VerifyExistenceAndRemoveFromList(List<string> alExisting,
389389
/// </summary>
390390
/// <param name="sBaseFileName"></param>
391391
/// <param name="fileEntries"></param>
392-
private static void VerifyFileConditions(string sBaseFileName, List<RollFileEntry> fileEntries)
392+
private static void VerifyFileConditions(string sBaseFileName, IList<RollFileEntry> fileEntries)
393393
{
394394
List<string> alExisting = GetExistingFiles(sBaseFileName);
395395

@@ -429,7 +429,7 @@ private static void VerifyFileConditions(string sBaseFileName, List<RollFileEntr
429429
/// <param name="sBaseFileName"></param>
430430
/// <param name="entry"></param>
431431
private static void VerifyPreConditions(string sBaseFileName, RollConditions entry)
432-
=> VerifyFileConditions(sBaseFileName, entry.GetPreLogFileEntries());
432+
=> VerifyFileConditions(sBaseFileName, entry.PreLogFileEntries);
433433

434434
/// <summary>
435435
/// Called after logging a message to check that all the expected files exist,
@@ -439,7 +439,7 @@ private static void VerifyPreConditions(string sBaseFileName, RollConditions ent
439439
/// <param name="sBaseFileName"></param>
440440
/// <param name="entry"></param>
441441
private static void VerifyPostConditions(string sBaseFileName, RollConditions entry)
442-
=> VerifyFileConditions(sBaseFileName, entry.GetPostLogFileEntries());
442+
=> VerifyFileConditions(sBaseFileName, entry.PostLogFileEntries);
443443

444444
/// <summary>
445445
/// Logs a message, verifying the expected message counts against the
@@ -672,7 +672,7 @@ private static RollConditions BuildTableEntry(string sBackupFiles,
672672
return new RollConditions(AddFinalElement(null, current), post);
673673
}
674674

675-
return new RollConditions(preCondition.GetPostLogFileEntries(), post);
675+
return new RollConditions(preCondition.PostLogFileEntries, post);
676676
}
677677

678678
/// <summary>
@@ -1808,7 +1808,7 @@ private static List<string> GetExistingFiles(string baseFilePath, bool preserveL
18081808
{
18091809
2 => TestMessage98Chars,
18101810
1 => TestMessage99Chars,
1811-
_ => throw new Exception("Unexpected Environment.NewLine.Length"),
1811+
_ => throw new InvalidOperationException("Unexpected Environment.NewLine.Length"),
18121812
};
18131813
}
18141814

@@ -1818,15 +1818,13 @@ public sealed class RollingFileAppenderSubClassTest : RollingFileAppender
18181818
[Test]
18191819
public void TestComputeCheckPeriod()
18201820
{
1821-
RollingFileAppender rfa = new();
1822-
1823-
Assert.That(rfa.ComputeCheckPeriod(".yyyy-MM-dd HH:mm"), Is.EqualTo(RollPoint.TopOfMinute), "TopOfMinute pattern");
1824-
Assert.That(rfa.ComputeCheckPeriod(".yyyy-MM-dd HH"), Is.EqualTo(RollPoint.TopOfHour), "TopOfHour pattern");
1825-
Assert.That(rfa.ComputeCheckPeriod(".yyyy-MM-dd tt"), Is.EqualTo(RollPoint.HalfDay), "HalfDay pattern");
1826-
Assert.That(rfa.ComputeCheckPeriod(".yyyy-MM-dd"), Is.EqualTo(RollPoint.TopOfDay), "TopOfDay pattern");
1827-
Assert.That(rfa.ComputeCheckPeriod(".yyyy-MM"), Is.EqualTo(RollPoint.TopOfMonth), "TopOfMonth pattern");
1821+
Assert.That(ComputeCheckPeriod(".yyyy-MM-dd HH:mm"), Is.EqualTo(RollPoint.TopOfMinute), "TopOfMinute pattern");
1822+
Assert.That(ComputeCheckPeriod(".yyyy-MM-dd HH"), Is.EqualTo(RollPoint.TopOfHour), "TopOfHour pattern");
1823+
Assert.That(ComputeCheckPeriod(".yyyy-MM-dd tt"), Is.EqualTo(RollPoint.HalfDay), "HalfDay pattern");
1824+
Assert.That(ComputeCheckPeriod(".yyyy-MM-dd"), Is.EqualTo(RollPoint.TopOfDay), "TopOfDay pattern");
1825+
Assert.That(ComputeCheckPeriod(".yyyy-MM"), Is.EqualTo(RollPoint.TopOfMonth), "TopOfMonth pattern");
18281826

18291827
// Test invalid roll point
1830-
Assert.That(rfa.ComputeCheckPeriod("..."), Is.EqualTo(RollPoint.InvalidRollPoint), "TopOfMonth pattern");
1828+
Assert.That(ComputeCheckPeriod("..."), Is.EqualTo(RollPoint.InvalidRollPoint), "TopOfMonth pattern");
18311829
}
18321830
}

src/log4net.Tests/Core/LoggingEventTest.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ private static readonly DateTime _localTime
3737
= new(2000, 7, 1, 0, 0, 0, 0, CultureInfo.InvariantCulture.Calendar, DateTimeKind.Local);
3838

3939
[Test]
40+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "CA2300:Do not use insecure deserializer BinaryFormatter")]
4041
public void SerializeDeserialize_BinaryFormatter()
4142
{
4243
Utils.InconclusiveOnMono();
@@ -86,6 +87,7 @@ public void SerializeDeserialize_BinaryFormatter()
8687
/// Loads and validates the cached serialized v2 event data from the log4net2-SerializeEvent directory.
8788
/// </summary>
8889
[Test]
90+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "CA2300:Do not use insecure deserializer BinaryFormatter")]
8991
public void DeserializeV2()
9092
{
9193
Utils.InconclusiveOnMono();

src/log4net.Tests/Util/PatternConverterTest.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ public class PropertyKeyCountPatternLayoutConverter : PatternLayoutConverter
136136
{
137137
public PropertyKeyCountPatternLayoutConverter() => MostRecentInstance = this;
138138

139+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods")]
139140
protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
140141
=> writer.Write(Properties!.GetKeys().Length);
141142

@@ -146,6 +147,7 @@ public class PropertyKeyCountPatternConverter : PatternConverter
146147
{
147148
public PropertyKeyCountPatternConverter() => MostRecentInstance = this;
148149

150+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods")]
149151
public override void Convert(TextWriter writer, object? state)
150152
=> writer.Write(Properties!.GetKeys().Length);
151153

src/log4net.globalconfig

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ dotnet_diagnostic.CA1027.severity = warning
5555
#CA1031: Do not catch general exception types
5656
dotnet_diagnostic.CA1031.severity = warning
5757

58-
#CA1034: Nested types should not be visible
59-
dotnet_diagnostic.CA1034.severity = warning
60-
6158
#CA1040: Avoid empty interfaces
6259
dotnet_diagnostic.CA1040.severity = warning
6360

@@ -461,7 +458,7 @@ dotnet_diagnostic.CA5402.severity = warning
461458
dotnet_diagnostic.CA5403.severity = warning
462459

463460
#IDE0010: Add missing cases to switch statement
464-
dotnet_diagnostic.IDE0010.severity = warning
461+
dotnet_diagnostic.IDE0010.severity = none
465462

466463
#IDE0067: Disposable object is never disposed
467464
dotnet_diagnostic.IDE0067.severity = warning
@@ -470,4 +467,4 @@ dotnet_diagnostic.IDE0067.severity = warning
470467
dotnet_diagnostic.IDE0068.severity = warning
471468

472469
#IDE0072: Add missing cases to switch expression
473-
dotnet_diagnostic.IDE0072.severity = warning
470+
dotnet_diagnostic.IDE0072.severity = none

src/log4net/Appender/AdoNetAppender.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ public void AddParameter(AdoNetAppenderParameter parameter)
450450
/// </remarks>
451451
protected virtual void SendBuffer(IDbTransaction? dbTran, LoggingEvent[] events)
452452
{
453+
events.EnsureNotNull();
453454
if (!string.IsNullOrWhiteSpace(CommandText))
454455
{
455456
using IDbCommand dbCmd = Connection.EnsureNotNull().CreateCommand();
@@ -522,7 +523,7 @@ protected virtual void Prepare(IDbCommand dbCmd)
522523
parameter.Prepare(dbCmd);
523524
}
524525

525-
dbCmd.Prepare();
526+
dbCmd.EnsureNotNull().Prepare();
526527
}
527528

528529
/// <summary>
@@ -883,7 +884,7 @@ public DbType DbType
883884
public virtual void Prepare(IDbCommand command)
884885
{
885886
// Create a new parameter
886-
IDbDataParameter param = command.CreateParameter();
887+
IDbDataParameter param = command.EnsureNotNull().CreateParameter();
887888

888889
// Set the parameter properties
889890
param.ParameterName = ParameterName;
@@ -923,7 +924,8 @@ public virtual void Prepare(IDbCommand command)
923924
public virtual void FormatValue(IDbCommand command, LoggingEvent loggingEvent)
924925
{
925926
// Lookup the parameter
926-
IDbDataParameter param = (IDbDataParameter)command.Parameters[ParameterName.EnsureNotNull()];
927+
IDbDataParameter param = command.EnsureNotNull().Parameters[ParameterName.EnsureNotNull()]
928+
.EnsureIs<IDbDataParameter>();
927929

928930
// Format the value
929931
object? formattedValue = Layout?.Format(loggingEvent);

src/log4net/Appender/AnsiColorTerminalAppender.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public virtual string Target
242242
/// </remarks>
243243
protected override void Append(LoggingEvent loggingEvent)
244244
{
245-
string loggingMessage = RenderLoggingEvent(loggingEvent);
245+
string loggingMessage = RenderLoggingEvent(loggingEvent.EnsureNotNull());
246246

247247
// see if there is a specified lookup.
248248
if (_levelMapping.Lookup(loggingEvent.Level) is LevelColors levelColors)

src/log4net/Appender/AppenderCollection.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -409,17 +409,17 @@ public virtual int Capacity
409409
/// <summary>
410410
/// Adds the elements of another <see cref="AppenderCollection"/> to the current <see cref="AppenderCollection"/>.
411411
/// </summary>
412-
/// <param name="x">The <see cref="AppenderCollection"/> whose elements should be added to the end of the current <see cref="AppenderCollection"/>.</param>
412+
/// <param name="collection">The <see cref="AppenderCollection"/> whose elements should be added to the end of the current <see cref="AppenderCollection"/>.</param>
413413
/// <returns>The new <see cref="Count"/> of the <see cref="AppenderCollection"/>.</returns>
414-
public virtual int AddRange(AppenderCollection x)
414+
public virtual int AddRange(AppenderCollection collection)
415415
{
416-
if (_count + x.Count >= _array.Length)
416+
if (_count + collection.EnsureNotNull().Count >= _array.Length)
417417
{
418-
EnsureCapacity(_count + x.Count);
418+
EnsureCapacity(_count + collection.Count);
419419
}
420420

421-
Array.Copy(x._array, 0, _array, _count, x.Count);
422-
_count += x.Count;
421+
Array.Copy(collection._array, 0, _array, _count, collection.Count);
422+
_count += collection.Count;
423423
_version++;
424424

425425
return _count;
@@ -428,17 +428,17 @@ public virtual int AddRange(AppenderCollection x)
428428
/// <summary>
429429
/// Adds the elements of a <see cref="IAppender"/> array to the current <see cref="AppenderCollection"/>.
430430
/// </summary>
431-
/// <param name="x">The <see cref="IAppender"/> array whose elements should be added to the end of the <see cref="AppenderCollection"/>.</param>
431+
/// <param name="array">The <see cref="IAppender"/> array whose elements should be added to the end of the <see cref="AppenderCollection"/>.</param>
432432
/// <returns>The new <see cref="Count"/> of the <see cref="AppenderCollection"/>.</returns>
433-
public virtual int AddRange(IAppender[] x)
433+
public virtual int AddRange(IAppender[] array)
434434
{
435-
if (_count + x.Length >= _array.Length)
435+
if (_count + array.EnsureNotNull().Length >= _array.Length)
436436
{
437-
EnsureCapacity(_count + x.Length);
437+
EnsureCapacity(_count + array.Length);
438438
}
439439

440-
Array.Copy(x, 0, _array, _count, x.Length);
441-
_count += x.Length;
440+
Array.Copy(array, 0, _array, _count, array.Length);
441+
_count += array.Length;
442442
_version++;
443443

444444
return _count;
@@ -447,18 +447,18 @@ public virtual int AddRange(IAppender[] x)
447447
/// <summary>
448448
/// Adds the elements of a <see cref="IAppender"/> collection to the current <see cref="AppenderCollection"/>.
449449
/// </summary>
450-
/// <param name="col">The <see cref="IAppender"/> collection whose elements should be added to the end of the <see cref="AppenderCollection"/>.</param>
450+
/// <param name="collection">The <see cref="IAppender"/> collection whose elements should be added to the end of the <see cref="AppenderCollection"/>.</param>
451451
/// <returns>The new <see cref="Count"/> of the <see cref="AppenderCollection"/>.</returns>
452-
public virtual int AddRange(ICollection col)
452+
public virtual int AddRange(ICollection collection)
453453
{
454-
if (_count + col.Count >= _array.Length)
454+
if (_count + collection.EnsureNotNull().Count >= _array.Length)
455455
{
456-
EnsureCapacity(_count + col.Count);
456+
EnsureCapacity(_count + collection.Count);
457457
}
458458

459-
foreach (object item in col)
459+
foreach (object item in collection)
460460
{
461-
Add((IAppender)item);
461+
Add(item.EnsureIs<IAppender>());
462462
}
463463

464464
return _count;
@@ -475,7 +475,7 @@ public virtual int AddRange(ICollection col)
475475
/// <returns>the array</returns>
476476
public virtual IAppender[] ToArray()
477477
{
478-
var resultArray = new IAppender[_count];
478+
IAppender[] resultArray = new IAppender[_count];
479479
if (_count > 0)
480480
{
481481
Array.Copy(_array, 0, resultArray, 0, _count);

src/log4net/Appender/AppenderSkeleton.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,12 @@ public void DoAppend(LoggingEvent loggingEvent)
332332
/// </remarks>
333333
public void DoAppend(LoggingEvent[] loggingEvents)
334334
{
335+
loggingEvents.EnsureNotNull();
336+
335337
// This lock is absolutely critical for correct formatting
336338
// of the message in a multi-threaded environment. Without
337339
// this, the message may be broken up into elements from
338340
// multiple thread contexts (like get the wrong thread ID).
339-
340341
lock (LockObj)
341342
{
342343
if (_isClosed)
@@ -414,7 +415,7 @@ public void DoAppend(LoggingEvent[] loggingEvents)
414415
/// </remarks>
415416
protected virtual bool FilterEvent(LoggingEvent loggingEvent)
416417
{
417-
if (!IsAsSevereAsThreshold(loggingEvent.Level))
418+
if (!IsAsSevereAsThreshold(loggingEvent.EnsureNotNull().Level))
418419
{
419420
return false;
420421
}
@@ -553,7 +554,7 @@ protected virtual void OnClose()
553554
/// </remarks>
554555
protected virtual void Append(LoggingEvent[] loggingEvents)
555556
{
556-
foreach (LoggingEvent loggingEvent in loggingEvents)
557+
foreach (LoggingEvent loggingEvent in loggingEvents.EnsureNotNull())
557558
{
558559
Append(loggingEvent);
559560
}
@@ -575,7 +576,7 @@ protected virtual void Append(LoggingEvent[] loggingEvents)
575576
/// </remarks>
576577
protected virtual void Append(IEnumerable<LoggingEvent> loggingEvents)
577578
{
578-
foreach (LoggingEvent loggingEvent in loggingEvents)
579+
foreach (LoggingEvent loggingEvent in loggingEvents.EnsureNotNull())
579580
{
580581
Append(loggingEvent);
581582
}
@@ -674,14 +675,15 @@ protected string RenderLoggingEvent(LoggingEvent loggingEvent)
674675
/// </remarks>
675676
protected void RenderLoggingEvent(TextWriter writer, LoggingEvent loggingEvent)
676677
{
678+
writer.EnsureNotNull();
677679
if (Layout is null)
678680
{
679681
throw new InvalidOperationException("A layout must be set");
680682
}
681683

682684
if (Layout.IgnoresException)
683685
{
684-
string? exceptionStr = loggingEvent.GetExceptionString();
686+
string? exceptionStr = loggingEvent.EnsureNotNull().GetExceptionString();
685687
if (!string.IsNullOrEmpty(exceptionStr))
686688
{
687689
// render the event and the exception

src/log4net/Appender/BufferingAppenderSkeleton.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ protected override void Append(LoggingEvent loggingEvent)
469469
/// </remarks>
470470
protected virtual void SendFromBuffer(LoggingEvent? firstLoggingEvent, CyclicBuffer buffer)
471471
{
472-
LoggingEvent[] bufferEvents = buffer.PopAll();
472+
LoggingEvent[] bufferEvents = buffer.EnsureNotNull().PopAll();
473473

474474
if (firstLoggingEvent is null)
475475
{

src/log4net/Appender/ColoredConsoleAppender.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ public virtual string Target
197197
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand, UnmanagedCode = true)]
198198
protected override void Append(LoggingEvent loggingEvent)
199199
{
200+
loggingEvent.EnsureNotNull();
200201
if (_consoleOutputWriter is not null)
201202
{
202203
IntPtr consoleHandle = GetStdHandle(_writeToErrorStream ? StdErrorHandle : StdOutputHandle);

0 commit comments

Comments
 (0)