Skip to content

SCAN4NET-96 and SCAN4NET-97: Fix logger timestamp duplication #2212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 25, 2024

Conversation

gregory-paidis-sonarsource
Copy link
Contributor

@gregory-paidis-sonarsource gregory-paidis-sonarsource commented Sep 25, 2024

public void AssertExpectedLastMessageRegex(string expected)
{
var lastMessage = GetLastMessage(outputWriter);
lastMessage.Should().MatchRegex(expected, "Expected message was not logged");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to match the timestamp abstractly, as it does not have a concrete, static value.

@@ -111,7 +111,7 @@ public void ResumeOutput()
}

public void LogWarning(string message, params object[] args) =>
Write(MessageType.Warning, GetFormattedMessage(Resources.Logger_WarningPrefix + message, args));
Write(MessageType.Warning, Resources.Logger_WarningPrefix + message, args);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were three bugs with this file:

  1. LogWarning AND LogUIWarning used to call GetFormattedMessage. Every invocation adds one more timestamp, and since the UI method calls the normal warning one, we would get 2 of them.
  2. Write would add one more GetFormattedMessage on the top, so we would get 3 of them.
  3. FlushOutput then would also call GetFormattedMessage, so we would get 4 of them.

My refactoring does the following two main steps:

  • The only place where GetFormattedMessage is allowed to be called is inside Write. This means every Log method falls-through to Write, where the formatting happens.
  • The FlushOutput does not call Write at all, but a method that is wrapped by Write, which already expects the message to be formatted.

Copy link

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@costin-zaharia-sonarsource costin-zaharia-sonarsource merged commit 8483352 into master Sep 25, 2024
21 checks passed
@costin-zaharia-sonarsource costin-zaharia-sonarsource deleted the greg/scan4net-96-and-97 branch September 25, 2024 12:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants