diff --git a/src/Core.UnitTests/Logging/LoggerContextManagerTests.cs b/src/Core.UnitTests/Logging/LoggerContextManagerTests.cs index 0a88ff492..1f8670409 100644 --- a/src/Core.UnitTests/Logging/LoggerContextManagerTests.cs +++ b/src/Core.UnitTests/Logging/LoggerContextManagerTests.cs @@ -31,11 +31,11 @@ public void MefCtor_CheckIsExported() => MefTestHelpers.CheckTypeCanBeImported(); [TestMethod] - public void MefCtor_CheckIsSingleton() => + public void MefCtor_CheckIsTransient() => MefTestHelpers.CheckIsNonSharedMefComponent(); [TestMethod] - public void EmptyContext() + public void DefaultCtor_EmptyContext() { var testSubject = new LoggerContextManager(); diff --git a/src/Core/Logging/LoggerBase.cs b/src/Core/Logging/LoggerBase.cs index 350799de4..8848e6236 100644 --- a/src/Core/Logging/LoggerBase.cs +++ b/src/Core/Logging/LoggerBase.cs @@ -71,31 +71,25 @@ private StringBuilder CreateStandardLogPrefix(MessageLevelContext context = defa AddStandardProperties(new StringBuilder(), context); private StringBuilder CreateDebugLogPrefix(MessageLevelContext context = default) => - AddStandardProperties(AppendProperty(new StringBuilder(), "DEBUG"), context); + AddStandardProperties(new StringBuilder().AppendProperty("DEBUG"), context); private StringBuilder AddStandardProperties(StringBuilder builder, MessageLevelContext context) { if (settingsProvider.IsThreadIdEnabled) { - AppendPropertyFormat(builder, "ThreadId {0}", Thread.CurrentThread.ManagedThreadId); + builder.AppendPropertyFormat("ThreadId {0}", Thread.CurrentThread.ManagedThreadId); } if (contextManager.GetFormattedContextOrNull(context) is var formatedContext && !string.IsNullOrEmpty(formatedContext)) { - AppendProperty(builder, formatedContext); + builder.AppendProperty(formatedContext); } if (settingsProvider.IsVerboseEnabled && contextManager.GetFormattedVerboseContextOrNull(context) is var formattedVerboseContext && !string.IsNullOrEmpty(formattedVerboseContext)) { - AppendProperty(builder, formattedVerboseContext); + builder.AppendProperty(formattedVerboseContext); } return builder; } - - private static StringBuilder AppendProperty(StringBuilder builder, string property) => - builder.Append('[').Append(property).Append(']').Append(' '); - - private static StringBuilder AppendPropertyFormat(StringBuilder builder, string property, params object[] args) => - builder.Append('[').AppendFormat(property, args).Append(']').Append(' '); } diff --git a/src/Core/Logging/StringBuilderLoggingExtensions.cs b/src/Core/Logging/StringBuilderLoggingExtensions.cs new file mode 100644 index 000000000..7c94eae54 --- /dev/null +++ b/src/Core/Logging/StringBuilderLoggingExtensions.cs @@ -0,0 +1,32 @@ +/* + * SonarLint for Visual Studio + * Copyright (C) 2016-2024 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +using System.Text; + +namespace SonarLint.VisualStudio.Core.Logging; + +internal static class StringBuilderLoggingExtensions +{ + public static StringBuilder AppendProperty(this StringBuilder builder, string property) => + builder.Append('[').Append(property).Append(']').Append(' '); + + public static StringBuilder AppendPropertyFormat(this StringBuilder builder, string property, params object[] args) => + builder.Append('[').AppendFormat(property, args).Append(']').Append(' '); +}