Skip to content

Commit d9bd997

Browse files
(#181) Use new .NET 9.0 Lock class
1 parent 6b37816 commit d9bd997

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

src/Stravaig.Extensions.Logging.Diagnostics/LogEntry.cs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
using System.Text;
66
using Microsoft.Extensions.Logging;
77

8+
#if NET9_0_OR_GREATER
9+
using System.Threading;
10+
#endif
11+
812
namespace Stravaig.Extensions.Logging.Diagnostics;
913

1014
/// <summary>
@@ -14,45 +18,49 @@ namespace Stravaig.Extensions.Logging.Diagnostics;
1418
public class LogEntry : IComparable<LogEntry>
1519
{
1620
private static int _sequence;
17-
private static readonly object SequenceSyncLock = new ();
21+
#if NET9_0_OR_GREATER
22+
private static readonly Lock SequenceSyncLock = new();
23+
#else
24+
private static readonly object SequenceSyncLock = new();
25+
#endif
1826

1927
private const string OriginalMessagePropertyName = "{OriginalFormat}";
20-
28+
2129
private readonly Lazy<IReadOnlyDictionary<string, object>> _lazyPropertyDictionary;
22-
30+
2331
/// <summary>
2432
/// The <see cref="T:Microsoft.Extensions.Logging.LogLevel"/> that the item was logged at.
2533
/// </summary>
2634
public LogLevel LogLevel { get; }
27-
35+
2836
/// <summary>
2937
/// An <see cref="T:Microsoft.Extensions.Logging.EventId"/> that identifies a logging event.
3038
/// </summary>
3139
public EventId EventId { get; }
32-
40+
3341
/// <summary>
3442
/// The entry to be written. Can be also an object.
3543
/// </summary>
3644
public object? State { get; }
37-
45+
3846
/// <summary>
3947
/// The <see cref="T:System.Exception"/> that was attached to the log.
4048
/// </summary>
4149
public Exception? Exception { get; }
42-
50+
4351
/// <summary>
4452
/// The formatted message.
4553
/// </summary>
4654
public string FormattedMessage { get; }
47-
55+
4856
/// <summary>
4957
/// The sequence number of the log message.
5058
/// </summary>
51-
/// <remarks>In a multi-threaded environment there may be gaps between adjacent log messages.</remarks>
59+
/// <remarks>In a multithreaded environment there may be gaps between adjacent log messages.</remarks>
5260
public int Sequence { get; }
53-
61+
5462
/// <summary>
55-
/// The time the log entry was created in UTC.
63+
/// The time the log entry was created in UTC.
5664
/// </summary>
5765
public DateTime TimestampUtc { get; }
5866

@@ -74,7 +82,7 @@ public DateTimeOffset TimestampLocal
7482
/// </summary>
7583
public IReadOnlyList<KeyValuePair<string, object>> Properties =>
7684
State as IReadOnlyList<KeyValuePair<string, object>> ?? Array.Empty<KeyValuePair<string, object>>();
77-
85+
7886
/// <summary>
7987
/// The properties, if any, for the log entry.
8088
/// </summary>
@@ -87,7 +95,7 @@ public DateTimeOffset TimestampLocal
8795
(string) Properties
8896
.FirstOrDefault(p => p.Key == OriginalMessagePropertyName)
8997
.Value;
90-
98+
9199
/// <summary>
92100
/// The category name of the log entry, if available.
93101
/// </summary>
@@ -178,4 +186,4 @@ public override string ToString()
178186
}
179187

180188
private string DebuggerDisplayString => $"[#{Sequence} @ {TimestampLocal:HH:mm:ss.fff zzz} {LogLevel} {CategoryName}] {FormattedMessage}";
181-
}
189+
}

src/Stravaig.Extensions.Logging.Diagnostics/Stravaig.Extensions.Logging.Diagnostics.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
Use version 3.x for .NET 6.0, 8.0 &amp; 9.0 onwards.
2323
</Description>
2424
<Nullable>enable</Nullable>
25-
<LangVersion>12</LangVersion>
25+
<LangVersion>13</LangVersion>
2626
</PropertyGroup>
2727
<ItemGroup>
2828
<None Include="stravaig-icon.png" Pack="true" PackagePath="/" />

src/Stravaig.Extensions.Logging.Diagnostics/TestCaptureLogger.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using Microsoft.Extensions.Logging;
5+
#if NET9_0_OR_GREATER
6+
using System.Threading;
7+
#endif
58

69
namespace Stravaig.Extensions.Logging.Diagnostics;
710

@@ -11,16 +14,18 @@ namespace Stravaig.Extensions.Logging.Diagnostics;
1114
/// </summary>
1215
public class TestCaptureLogger : ITestCaptureLogger
1316
{
14-
private readonly List<LogEntry> _logs;
15-
private readonly object _syncRoot;
17+
private readonly List<LogEntry> _logs = [];
18+
#if NET9_0_OR_GREATER
19+
private readonly Lock _listGuard = new();
20+
#else
21+
private readonly object _listGuard = new object();
22+
#endif
1623

1724
/// <summary>
1825
/// Initialises a new instance of the <see cref="T:Stravaig.Extensions.Logging.Diagnostics.TestCaptureLogger"/> class.
1926
/// </summary>
2027
public TestCaptureLogger()
2128
{
22-
_logs = new List<LogEntry>();
23-
_syncRoot = new object();
2429
CategoryName = string.Empty;
2530
}
2631

@@ -29,7 +34,6 @@ public TestCaptureLogger()
2934
/// </summary>
3035
/// <param name="categoryName">The name of the category</param>
3136
public TestCaptureLogger(string categoryName)
32-
: this()
3337
{
3438
CategoryName = categoryName;
3539
}
@@ -42,7 +46,7 @@ public TestCaptureLogger(string categoryName)
4246
/// <inheritdoc />
4347
public IReadOnlyList<LogEntry> GetLogs()
4448
{
45-
lock (_syncRoot)
49+
lock (_listGuard)
4650
{
4751
_logs.Sort();
4852
return _logs.ToArray();
@@ -52,7 +56,7 @@ public IReadOnlyList<LogEntry> GetLogs()
5256
/// <inheritdoc />
5357
public IReadOnlyList<LogEntry> GetLogs(Func<LogEntry, bool> predicate)
5458
{
55-
lock (_syncRoot)
59+
lock (_listGuard)
5660
{
5761
return _logs
5862
.Where(predicate)
@@ -78,7 +82,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
7882
{
7983
var formattedMessage = formatter(state, exception);
8084
var logEntry = CreateLogEntry(logLevel, eventId, state, exception, formattedMessage);
81-
lock (_syncRoot)
85+
lock (_listGuard)
8286
{
8387
_logs.Add(logEntry);
8488
}
@@ -131,7 +135,7 @@ public void Dispose()
131135
/// <inheritdoc />
132136
public void Reset()
133137
{
134-
lock (_syncRoot)
138+
lock (_listGuard)
135139
{
136140
_logs.Clear();
137141
}

0 commit comments

Comments
 (0)