From 5590bc521fc7182d34a24225aa10867b33caeeb2 Mon Sep 17 00:00:00 2001 From: Anirudh Agnihotry Date: Wed, 24 Jul 2019 17:46:40 -0700 Subject: [PATCH] Only removing logs which throw invalid parameters exception from eventLogs (dotnet/corefx#39705) * only removing logs which throw invalid parametert exception from eventlogs * using ordinalIgnoreCase, const name and improving comment Commit migrated from https://github.com/dotnet/corefx/commit/70aeda2b5b7110ead0ea69dc83f3f47e5728db06 --- .../src/System/Diagnostics/EventLog.cs | 6 ++++++ .../tests/EventLogTests/EventLogTests.cs | 11 ++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/EventLog.cs b/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/EventLog.cs index c9c90a4..3a14266 100644 --- a/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/EventLog.cs +++ b/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/EventLog.cs @@ -667,6 +667,12 @@ namespace System.Diagnostics handle.Close(); logs.Add(log); } + else if (Marshal.GetLastWin32Error() != Interop.Errors.ERROR_INVALID_PARAMETER) + { + // This api should return the list of all event logs present on the system even if the current user can't open the log. + // Windows returns ERROR_INVALID_PARAMETER for special keys which were added in RS5+ but do not represent actual event logs. + logs.Add(log); + } } return logs.ToArray(); diff --git a/src/libraries/System.Diagnostics.EventLog/tests/EventLogTests/EventLogTests.cs b/src/libraries/System.Diagnostics.EventLog/tests/EventLogTests/EventLogTests.cs index 37ea855..e4b0f40 100644 --- a/src/libraries/System.Diagnostics.EventLog/tests/EventLogTests/EventLogTests.cs +++ b/src/libraries/System.Diagnostics.EventLog/tests/EventLogTests/EventLogTests.cs @@ -4,6 +4,7 @@ using System.Globalization; using System.IO; +using System.Linq; using Xunit; namespace System.Diagnostics.Tests @@ -349,7 +350,7 @@ namespace System.Diagnostics.Tests } } - [ConditionalFact(typeof(Helpers), nameof(Helpers.SupportsEventLogs))] + [ConditionalFact(typeof(Helpers), nameof(Helpers.IsElevatedAndSupportsEventLogs))] [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)] public void GetEventLogEntriesTest() { @@ -359,5 +360,13 @@ namespace System.Diagnostics.Tests Assert.True(Helpers.RetryOnWin7(() => eventLog.Entries.Count) >= 0); } } + + [ConditionalFact(typeof(Helpers), nameof(Helpers.SupportsEventLogs))] + [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)] + public void GetEventLogContainsSecurityLogTest() + { + EventLog[] eventlogs = EventLog.GetEventLogs(); + Assert.True(eventlogs.Select(t => t.Log).Contains("Security", StringComparer.OrdinalIgnoreCase)); + } } } -- 2.7.4