Skip to content

Commit 340bf9d

Browse files
SCAN4NET-100 Remove timestamps from UI Warnings (#2214)
1 parent 9ace0f0 commit 340bf9d

File tree

2 files changed

+23
-40
lines changed

2 files changed

+23
-40
lines changed

Tests/SonarScanner.MSBuild.Common.Test/ILoggerTests.cs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -318,20 +318,21 @@ public void ConsoleLogger_SuspendAndResume()
318318
[TestMethod]
319319
public void ConsoleLogger_WriteUIWarnings_GenerateFile()
320320
{
321+
const string prefixRegex = @"\d{2}:\d{2}:\d{2}(.\d{1,3})? WARNING:";
321322
using var output = new OutputCaptureScope();
322-
var logger = new ConsoleLogger(includeTimestamp: false, fileWrapper);
323+
var logger = new ConsoleLogger(includeTimestamp: true, fileWrapper);
323324

324325
logger.LogUIWarning("uiWarn1");
325-
output.AssertLastMessageEndsWith("uiWarn1"); // UI warnings should also be logged in the console
326+
output.AssertExpectedLastMessageRegex($"{prefixRegex} uiWarn1");
326327

327328
logger.LogUIWarning("uiWarn2", null);
328-
output.AssertLastMessageEndsWith("uiWarn2");
329+
output.AssertExpectedLastMessageRegex($"{prefixRegex} uiWarn2");
329330

330331
logger.LogUIWarning("uiWarn3 {0}", "xxx");
331-
output.AssertLastMessageEndsWith("uiWarn3 xxx");
332+
output.AssertExpectedLastMessageRegex($"{prefixRegex} uiWarn3 xxx");
332333

333334
const string outputDir = "outputDir";
334-
logger.WriteUIWarnings(outputDir);
335+
logger.WriteUIWarnings(outputDir); // this should not contain any timestamps.
335336
fileWrapper.Received(1).WriteAllText(
336337
Path.Combine(outputDir, FileConstants.UIWarningsFileName),
337338
"""
@@ -349,23 +350,6 @@ public void ConsoleLogger_WriteUIWarnings_GenerateFile()
349350
""");
350351
}
351352

352-
[TestMethod]
353-
public void ConsoleLogger_WriteUIWarnings_ContainsTimeStampOnce()
354-
{
355-
const string prefixRegex = @"\d{2}:\d{2}:\d{2}(.\d{1,3})? WARNING:";
356-
using var output = new OutputCaptureScope();
357-
var logger = new ConsoleLogger(includeTimestamp: true, fileWrapper);
358-
359-
logger.LogUIWarning("uiWarn1");
360-
output.AssertExpectedLastMessageRegex($"{prefixRegex} uiWarn1");
361-
362-
logger.LogUIWarning("uiWarn2", null);
363-
output.AssertExpectedLastMessageRegex($"{prefixRegex} uiWarn2");
364-
365-
logger.LogUIWarning("uiWarn3 {0}", "xxx");
366-
output.AssertExpectedLastMessageRegex($"{prefixRegex} uiWarn3 xxx");
367-
}
368-
369353
[TestMethod]
370354
public void ILogger_Log_Debug()
371355
{

src/SonarScanner.MSBuild.Common/ConsoleLogger.cs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public void LogInfo(string message, params object[] args) =>
124124

125125
public void LogUIWarning(string message, params object[] args)
126126
{
127-
uiWarnings.Add(GetFormattedMessage(message, args));
127+
uiWarnings.Add(FormatMessage(message, args));
128128
LogWarning(message, args);
129129
}
130130

@@ -137,22 +137,6 @@ public void WriteUIWarnings(string outputFolder)
137137
}
138138
}
139139

140-
private string GetFormattedMessage(string message, params object[] args)
141-
{
142-
var finalMessage = message;
143-
if (args is not null && args.Length > 0)
144-
{
145-
finalMessage = string.Format(CultureInfo.CurrentCulture, finalMessage ?? string.Empty, args);
146-
}
147-
148-
if (IncludeTimestamp)
149-
{
150-
finalMessage = string.Format(CultureInfo.CurrentCulture, "{0} {1}", DateTime.Now.ToString("HH:mm:ss.FFF",
151-
CultureInfo.InvariantCulture), finalMessage);
152-
}
153-
return finalMessage;
154-
}
155-
156140
private void FlushOutput()
157141
{
158142
Debug.Assert(isOutputSuspended, "Not expecting FlushOutput to be called unless output is currently suspended");
@@ -170,7 +154,7 @@ private void FlushOutput()
170154
/// Formats a message and writes or records it.
171155
/// </summary>
172156
private void Write(MessageType messageType, string message, params object[] args) =>
173-
WriteFormatted(messageType, GetFormattedMessage(message, args));
157+
WriteFormatted(messageType, FormatAndTimestampMessage(message, args));
174158

175159
/// <summary>
176160
/// Either writes the message to the output stream, or records it
@@ -196,6 +180,21 @@ private void WriteFormatted(MessageType messageType, string formatted)
196180
}
197181
}
198182

183+
private string FormatAndTimestampMessage(string message, params object[] args)
184+
{
185+
var formatted = FormatMessage(message, args);
186+
if (IncludeTimestamp)
187+
{
188+
formatted = string.Format(CultureInfo.CurrentCulture, "{0} {1}", DateTime.Now.ToString("HH:mm:ss.FFF", CultureInfo.InvariantCulture), formatted);
189+
}
190+
return formatted;
191+
}
192+
193+
private static string FormatMessage(string message, params object[] args) =>
194+
args is not null && args.Length > 0
195+
? string.Format(CultureInfo.CurrentCulture, message ?? string.Empty, args)
196+
: message;
197+
199198
private static ConsoleColor GetConsoleColor(MessageType messageType) =>
200199
messageType switch
201200
{

0 commit comments

Comments
 (0)