Skip to content

Fix flaky EventLogCollector test: ensure deterministic event log entries#15607

Merged
nohwnd merged 1 commit into
microsoft:mainfrom
nohwnd:fix-eventlog-test
Mar 30, 2026
Merged

Fix flaky EventLogCollector test: ensure deterministic event log entries#15607
nohwnd merged 1 commit into
microsoft:mainfrom
nohwnd:fix-eventlog-test

Conversation

@nohwnd
Copy link
Copy Markdown
Member

@nohwnd nohwnd commented Mar 27, 2026

Problem

The test \WriteEventLogEntriesToXmlFileShouldWriteLogEntryIfPresent\ in \EventLogXmlWriterTests.cs\ was flaky for two reasons:

  1. Empty event log: It assumed the Application event log always has entries (\�ventLog.Entries[eventLog.Entries.Count - 1]), which fails on clean/quiet machines with an empty Application log.

  2. Newline serialization mismatch: It used \XElement\ to predict how \DataSet.WriteXml\ would serialize the message. \XElement.ToString()\ normalizes lone \n\ to \r\n, but \DataSet.WriteXml\ preserves the original \n, causing \string.Contains\ to return false for messages with \n\ line endings.

Fix

  • Fall back to System log: Added \GetLastEventLogEntry\ helper that tries the Application log first, then falls back to the System log (which always has entries on a running Windows machine).
  • Manual XML escaping: Replaced \XElement-based serialization with direct \string.Replace\ for &, <, >\ — the same escaping \XmlWriter\ applies to text content, without the unintended newline normalization.

Closes #15601

Closes microsoft#15601

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 27, 2026 16:38
@nohwnd
Copy link
Copy Markdown
Member Author

nohwnd commented Mar 27, 2026

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@nohwnd
Copy link
Copy Markdown
Member Author

nohwnd commented Mar 30, 2026

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@nohwnd nohwnd added the 🚢 Ship it! Add to PRs where owner approves automated PR, but cannot approve because they "wrote it". label Mar 30, 2026
@nohwnd nohwnd merged commit 400f2c5 into microsoft:main Mar 30, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🚢 Ship it! Add to PRs where owner approves automated PR, but cannot approve because they "wrote it".

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EventLogCollector test flaky: WriteEventLogEntriesToXmlFileShouldWriteLogEntryIfPresent depends on machine state

1 participant