From 507c821f66877f521ffc52ff079d9c7eb48f54da Mon Sep 17 00:00:00 2001 From: Mole Date: Fri, 19 Mar 2021 13:19:39 +0100 Subject: [PATCH] Create method for generating log message And remove forgotten comments. --- src/Umbraco.Core/Scoping/Scope.cs | 22 ++++++++++++++------- src/Umbraco.Tests/Scoping/ScopeUnitTests.cs | 2 -- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/Umbraco.Core/Scoping/Scope.cs b/src/Umbraco.Core/Scoping/Scope.cs index 8075d9b165..3a11e0661e 100644 --- a/src/Umbraco.Core/Scoping/Scope.cs +++ b/src/Umbraco.Core/Scoping/Scope.cs @@ -368,14 +368,8 @@ namespace Umbraco.Core.Scoping // Since we're only reading we don't have to be in a lock if (ReadLocks?.Count > 0 || WriteLocks?.Count > 0) { - // Dump the dicts into a message for the locks. - StringBuilder builder = new StringBuilder(); - builder.AppendLine($"Lock counters aren't empty, suggesting a scope hasn't been properly disposed, parent id: {InstanceId}"); - WriteLockDictionaryToString(ReadLocks, builder, "read locks"); - WriteLockDictionaryToString(WriteLocks, builder, "write locks"); - var exception = new InvalidOperationException($"All scopes has not been disposed from parent scope: {InstanceId}, see log for more details."); - _logger.Error(exception, builder.ToString()); + _logger.Error(exception, GenerateUnclearedScopesLogMessage()); throw exception; } } @@ -399,6 +393,20 @@ namespace Umbraco.Core.Scoping GC.SuppressFinalize(this); } + /// + /// Generates a log message with all scopes that hasn't cleared their locks, including how many, and what locks they have requested. + /// + /// Log message. + private string GenerateUnclearedScopesLogMessage() + { + // Dump the dicts into a message for the locks. + StringBuilder builder = new StringBuilder(); + builder.AppendLine($"Lock counters aren't empty, suggesting a scope hasn't been properly disposed, parent id: {InstanceId}"); + WriteLockDictionaryToString(ReadLocks, builder, "read locks"); + WriteLockDictionaryToString(WriteLocks, builder, "write locks"); + return builder.ToString(); + } + /// /// Writes a locks dictionary to a for logging purposes. /// diff --git a/src/Umbraco.Tests/Scoping/ScopeUnitTests.cs b/src/Umbraco.Tests/Scoping/ScopeUnitTests.cs index cb0a1c1bea..038376f71c 100644 --- a/src/Umbraco.Tests/Scoping/ScopeUnitTests.cs +++ b/src/Umbraco.Tests/Scoping/ScopeUnitTests.cs @@ -94,7 +94,6 @@ namespace Umbraco.Tests.Scoping outerScope.Complete(); } - // Since we request the ReadLock after the innerScope has been dispose, the key has been removed from the dictionary, and we fetch it again. syntaxProviderMock.Verify(x => x.WriteLock(It.IsAny(), Constants.Locks.Languages), Times.Once); syntaxProviderMock.Verify(x => x.WriteLock(It.IsAny(), Constants.Locks.ContentTree), Times.Once); } @@ -223,7 +222,6 @@ namespace Umbraco.Tests.Scoping outerScope.Complete(); } - // Since we request the ReadLock after the innerScope has been dispose, the key has been removed from the dictionary, and we fetch it again. syntaxProviderMock.Verify(x => x.ReadLock(It.IsAny(), Constants.Locks.Languages), Times.Once); syntaxProviderMock.Verify(x => x.ReadLock(It.IsAny(), Constants.Locks.ContentTree), Times.Once); }