From 58550c2ca2b85af19b26b309d72b69e6f437b0fa Mon Sep 17 00:00:00 2001 From: Ben Mazzarol Date: Sat, 11 Mar 2023 12:38:22 +0800 Subject: [PATCH] fix(logging): Replace locks with concurrent dict [ci] Simplified the scopes in dummy logger to just use a concurrent dictionary ignoring the value side. Fixes the invalid lock code and correct analyser error raised. --- BunsenBurner.Logging/DummyLogger.cs | 26 +++++++++----------------- commitlint.config.js | 4 ++-- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/BunsenBurner.Logging/DummyLogger.cs b/BunsenBurner.Logging/DummyLogger.cs index 33ffabe..fdadbdc 100644 --- a/BunsenBurner.Logging/DummyLogger.cs +++ b/BunsenBurner.Logging/DummyLogger.cs @@ -1,4 +1,5 @@ using System.Collections; +using System.Collections.Concurrent; using System.Diagnostics.CodeAnalysis; using Microsoft.Extensions.Logging; @@ -13,7 +14,7 @@ public sealed record DummyLogger : ILogger, IEnumerable, IDisp private readonly string _ownerClassName; private readonly LogMessageStore _store; private readonly Sink? _sink; - private IEnumerable _scopes = Enumerable.Empty(); + private readonly ConcurrentDictionary _scopes = new(); internal DummyLogger(LogMessageStore store, string ownerClassName, Sink? sink) { @@ -37,7 +38,7 @@ public void Log( eventId, exception, formatter(state, exception), - _scopes.Select(x => x.State) + _scopes.Keys.Select(x => x.State) ); _store.Log(message); _sink?.Write(message); @@ -49,22 +50,15 @@ public void Log( /// public IDisposable BeginScope(TState state) where TState : notnull { - lock (_scopes) - { - var scope = new Scope(state, this); - _scopes = _scopes.Append(scope); - return scope; - } + var scope = new Scope(state, this); + _scopes.AddOrUpdate(scope, _ => byte.MinValue, (_, b) => b); + return scope; } private sealed record Scope(object State, DummyLogger Parent) : IDisposable { [ExcludeFromCodeCoverage] - public void Dispose() - { - lock (Parent._scopes) - Parent._scopes = Parent._scopes.Where(x => x != this); - } + public void Dispose() => Parent._scopes.TryRemove(this, out _); } /// @@ -76,11 +70,9 @@ public void Dispose() /// public void Dispose() { - foreach (var scope in _scopes) - { + foreach (var scope in _scopes.Keys) scope.Dispose(); - } - _scopes = Enumerable.Empty(); + _scopes.Clear(); } } diff --git a/commitlint.config.js b/commitlint.config.js index fb78885..f8f2676 100644 --- a/commitlint.config.js +++ b/commitlint.config.js @@ -1,8 +1,8 @@ module.exports = { extends: ['@commitlint/config-conventional'], rules: { - "header-case": [1, "always", "sentence-case"], - "header-max-length": [2, "always", 100], + "subject-case": [0, 'never'], + "subject-max-length": [2, "always", 100], "scope-enum": _ => [ 2, "always",