Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
georgii-borovinskikh-sonarsource committed Jan 6, 2025
1 parent b541115 commit 55280f0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/Core.UnitTests/Logging/LoggerContextManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ public void MefCtor_CheckIsExported() =>
MefTestHelpers.CheckTypeCanBeImported<LoggerContextManager, ILoggerContextManager>();

[TestMethod]
public void MefCtor_CheckIsSingleton() =>
public void MefCtor_CheckIsTransient() =>
MefTestHelpers.CheckIsNonSharedMefComponent<LoggerContextManager>();

[TestMethod]
public void EmptyContext()
public void DefaultCtor_EmptyContext()
{
var testSubject = new LoggerContextManager();

Expand Down
14 changes: 4 additions & 10 deletions src/Core/Logging/LoggerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(' ');
}
32 changes: 32 additions & 0 deletions src/Core/Logging/StringBuilderLoggingExtensions.cs
Original file line number Diff line number Diff line change
@@ -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(' ');
}

0 comments on commit 55280f0

Please sign in to comment.