Fix flaky EventLogCollector test: ensure deterministic event log entries by nohwnd · Pull Request #15607 · microsoft/vstest (original) (raw)
Problem
The test \WriteEventLogEntriesToXmlFileShouldWriteLogEntryIfPresent\ in \EventLogXmlWriterTests.cs\ was flaky for two reasons:
- 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.
- 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