Skip to content

Commit f320793

Browse files
authored
Merge pull request #201 from apache/Feature/NetAnalyzers
Enable Microsoft.CodeAnalysis.NetAnalyzers
2 parents b008475 + 6356bfb commit f320793

File tree

185 files changed

+2663
-2773
lines changed

Some content is hidden

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

185 files changed

+2663
-2773
lines changed

src/Directory.Build.props

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,25 @@
66
<LangVersion>latest</LangVersion>
77
<Nullable>Enable</Nullable>
88
<WarningsAsErrors>nullable</WarningsAsErrors>
9+
<AnalysisLevel>8</AnalysisLevel>
10+
<RunAnalyzersDuringBuild>true</RunAnalyzersDuringBuild>
11+
<RunAnalyzersDuringLiveAnalysis>true</RunAnalyzersDuringLiveAnalysis>
12+
<_SkipUpgradeNetAnalyzersNuGetWarning>true</_SkipUpgradeNetAnalyzersNuGetWarning>
13+
<AccelerateBuildsInVisualStudio>true</AccelerateBuildsInVisualStudio>
914
</PropertyGroup>
1015
<PropertyGroup Label="Package Versions">
1116
<SystemConfigurationConfigurationManagerPackageVersion>4.5.0</SystemConfigurationConfigurationManagerPackageVersion>
1217
<MicrosoftSourceLinkGitHubPackageVersion>8.0.0</MicrosoftSourceLinkGitHubPackageVersion>
18+
<!-- Analyzer packages -->
19+
<MicrosoftNetAnalyzersPackageVersion>8.0.0</MicrosoftNetAnalyzersPackageVersion>
1320
<!-- Test Packages -->
1421
<MicrosoftNetTestSdkPackageVersion>17.11.1</MicrosoftNetTestSdkPackageVersion>
1522
<NUnitAnalyzersPackageVersion>4.3.0</NUnitAnalyzersPackageVersion>
1623
<NUnitPackageVersion>4.2.2</NUnitPackageVersion>
1724
<NUnit3TestAdapterPackageVersion>4.6.0</NUnit3TestAdapterPackageVersion>
1825
<QuackersTestLoggerPackageVersion>1.0.25</QuackersTestLoggerPackageVersion>
1926
</PropertyGroup>
27+
<ItemGroup>
28+
<GlobalAnalyzerConfigFiles Include="$(MSBuildThisFileDirectory)\log4net.globalconfig" />
29+
</ItemGroup>
2030
</Project>

src/log4net.Tests/Appender/AdoNet/Log4NetCommand.cs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
namespace log4net.Tests.Appender.AdoNet;
2626

27-
public class Log4NetCommand : IDbCommand
27+
internal sealed class Log4NetCommand : IDbCommand
2828
{
2929
public Log4NetCommand()
3030
{
@@ -48,10 +48,7 @@ public int ExecuteNonQuery()
4848

4949
public int ExecuteNonQueryCount { get; private set; }
5050

51-
public IDbDataParameter CreateParameter()
52-
{
53-
return new Log4NetParameter();
54-
}
51+
public IDbDataParameter CreateParameter() => new Log4NetParameter();
5552

5653
#pragma warning disable CS8766 // Nullability of reference types in return type doesn't match implicitly implemented member (possibly because of nullability attributes).
5754
public string? CommandText { get; set; }
@@ -68,25 +65,13 @@ public void Prepare()
6865

6966
public static Log4NetCommand? MostRecentInstance { get; private set; }
7067

71-
public void Cancel()
72-
{
73-
throw new NotImplementedException();
74-
}
68+
public void Cancel() => throw new NotImplementedException();
7569

76-
public IDataReader ExecuteReader()
77-
{
78-
throw new NotImplementedException();
79-
}
70+
public IDataReader ExecuteReader() => throw new NotImplementedException();
8071

81-
public IDataReader ExecuteReader(CommandBehavior behavior)
82-
{
83-
throw new NotImplementedException();
84-
}
72+
public IDataReader ExecuteReader(CommandBehavior behavior) => throw new NotImplementedException();
8573

86-
public object ExecuteScalar()
87-
{
88-
throw new NotImplementedException();
89-
}
74+
public object ExecuteScalar() => throw new NotImplementedException();
9075

9176
public IDbConnection? Connection
9277
{

src/log4net.Tests/Appender/AdoNet/Log4NetConnection.cs

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,62 +21,40 @@
2121

2222
using System;
2323
using System.Data;
24+
using System.Diagnostics.CodeAnalysis;
2425

2526
namespace log4net.Tests.Appender.AdoNet;
2627

27-
public class Log4NetConnection : IDbConnection
28+
[SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses", Justification = "Reflection")]
29+
internal sealed class Log4NetConnection : IDbConnection
2830
{
2931
private bool _open;
3032

31-
public Log4NetConnection()
32-
{
33-
MostRecentInstance = this;
34-
}
33+
public Log4NetConnection() => MostRecentInstance = this;
3534

36-
public void Close()
37-
{
38-
_open = false;
39-
}
35+
public void Close() => _open = false;
4036

4137
public ConnectionState State => _open ? ConnectionState.Open : ConnectionState.Closed;
4238

4339
#pragma warning disable CS8766 // Nullability of reference types in return type doesn't match implicitly implemented member (possibly because of nullability attributes).
4440
public string? ConnectionString { get; set; }
4541
#pragma warning restore CS8766
4642

47-
public IDbTransaction BeginTransaction()
48-
{
49-
return new Log4NetTransaction();
50-
}
43+
public IDbTransaction BeginTransaction() => new Log4NetTransaction();
5144

52-
public IDbCommand CreateCommand()
53-
{
54-
return new Log4NetCommand();
55-
}
45+
public IDbCommand CreateCommand() => new Log4NetCommand();
5646

57-
public void Open()
58-
{
59-
_open = true;
60-
}
47+
public void Open() => _open = true;
6148

6249
public static Log4NetConnection? MostRecentInstance { get; private set; }
6350

64-
public IDbTransaction BeginTransaction(IsolationLevel il)
65-
{
66-
throw new NotImplementedException();
67-
}
51+
public IDbTransaction BeginTransaction(IsolationLevel il) => throw new NotImplementedException();
6852

69-
public void ChangeDatabase(string databaseName)
70-
{
71-
throw new NotImplementedException();
72-
}
53+
public void ChangeDatabase(string databaseName) => throw new NotImplementedException();
7354

7455
public int ConnectionTimeout => throw new NotImplementedException();
7556

7657
public string Database => throw new NotImplementedException();
7758

78-
public void Dispose()
79-
{
80-
throw new NotImplementedException();
81-
}
59+
public void Dispose() => throw new NotImplementedException();
8260
}

src/log4net.Tests/Appender/AdoNet/Log4NetParameterCollection.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
namespace log4net.Tests.Appender.AdoNet;
2828

29-
public class Log4NetParameterCollection : CollectionBase, IDataParameterCollection
29+
internal sealed class Log4NetParameterCollection : CollectionBase, IDataParameterCollection
3030
{
3131
private readonly Dictionary<string, int> _parameterNameToIndex = new(StringComparer.Ordinal);
3232

@@ -55,13 +55,7 @@ public object this[string parameterName]
5555
set => InnerList[IndexOf(parameterName)] = value;
5656
}
5757

58-
public void RemoveAt(string parameterName)
59-
{
60-
throw new NotImplementedException();
61-
}
58+
public void RemoveAt(string parameterName) => throw new NotImplementedException();
6259

63-
public bool Contains(string parameterName)
64-
{
65-
throw new NotImplementedException();
66-
}
60+
public bool Contains(string parameterName) => throw new NotImplementedException();
6761
}

src/log4net.Tests/Appender/AdoNet/Log4NetTransaction.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
namespace log4net.Tests.Appender.AdoNet;
2626

27-
public class Log4NetTransaction : IDbTransaction
27+
internal sealed class Log4NetTransaction : IDbTransaction
2828
{
2929
public void Commit()
3030
{
@@ -40,8 +40,5 @@ public void Rollback()
4040

4141
public IsolationLevel IsolationLevel => throw new NotImplementedException();
4242

43-
public void Dispose()
44-
{
45-
throw new NotImplementedException();
46-
}
43+
public void Dispose() => throw new NotImplementedException();
4744
}

src/log4net.Tests/Appender/CountingAppender.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,11 @@ public class CountingAppender : AppenderSkeleton
4444
/// <summary>
4545
/// Reset the counter to zero
4646
/// </summary>
47-
public void ResetCounter()
48-
{
49-
Counter = 0;
50-
}
47+
public void ResetCounter() => Counter = 0;
5148

5249
/// <summary>
5350
/// Registers how many times the method has been called.
5451
/// </summary>
55-
/// <param name="logEvent">The logging event.</param>
56-
protected override void Append(LoggingEvent logEvent)
57-
{
58-
Counter++;
59-
}
60-
}
52+
/// <param name="loggingEvent">The logging event.</param>
53+
protected override void Append(LoggingEvent loggingEvent) => Counter++;
54+
}

src/log4net.Tests/Appender/EventRaisingAppender.cs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,9 @@ namespace log4net.Tests.Appender;
2727
/// <summary>
2828
/// Provides data for the <see cref="EventRaisingAppender.LoggingEventAppended"/> event.
2929
/// </summary>
30-
/// <seealso cref="System.EventArgs" />
31-
public class LoggingEventEventArgs : EventArgs
30+
public class LoggingEventEventArgs(LoggingEvent loggingEvent) : EventArgs
3231
{
33-
public LoggingEvent LoggingEvent { get; private set; }
34-
35-
public LoggingEventEventArgs(LoggingEvent loggingEvent)
36-
{
37-
if (loggingEvent is null)
38-
{
39-
throw new ArgumentNullException(nameof(loggingEvent));
40-
}
41-
42-
LoggingEvent = loggingEvent;
43-
}
32+
public LoggingEvent LoggingEvent { get; } = loggingEvent ?? throw new ArgumentNullException(nameof(loggingEvent));
4433
}
4534

4635
/// <summary>
@@ -55,18 +44,13 @@ public class EventRaisingAppender : log4net.Appender.IAppender
5544
public event EventHandler<LoggingEventEventArgs>? LoggingEventAppended;
5645

5746
protected void OnLoggingEventAppended(LoggingEventEventArgs e)
58-
{
59-
LoggingEventAppended?.Invoke(this, e);
60-
}
47+
=> LoggingEventAppended?.Invoke(this, e);
6148

6249
public void Close()
63-
{
64-
}
50+
{ }
6551

6652
public void DoAppend(LoggingEvent loggingEvent)
67-
{
68-
OnLoggingEventAppended(new LoggingEventEventArgs(loggingEvent));
69-
}
53+
=> OnLoggingEventAppended(new LoggingEventEventArgs(loggingEvent));
7054

7155
public string Name { get; set; } = string.Empty;
72-
}
56+
}

src/log4net.Tests/Appender/RollingFileAppenderTest.cs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ private static void CreateFile(int iFileNumber)
170170
{
171171
fileStream.Close();
172172
}
173-
catch
173+
catch (Exception e) when (e is not null)
174174
{
175175
// Ignore
176176
}
@@ -250,9 +250,9 @@ private static void DeleteTestFiles()
250250
Debug.WriteLine("Deleting test file " + sFile);
251251
File.Delete(sFile);
252252
}
253-
catch (Exception ex)
253+
catch (Exception e) when (e is not null)
254254
{
255-
Debug.WriteLine("Exception while deleting test file " + ex);
255+
Debug.WriteLine("Exception while deleting test file " + e);
256256
}
257257
}
258258
}
@@ -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
}

0 commit comments

Comments
 (0)