Only removing logs which throw invalid parameters exception from eventLogs (dotnet...
authorAnirudh Agnihotry <anirudhagnihotry098@gmail.com>
Thu, 25 Jul 2019 00:46:40 +0000 (17:46 -0700)
committerGitHub <noreply@github.com>
Thu, 25 Jul 2019 00:46:40 +0000 (17:46 -0700)
* 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/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/EventLog.cs
src/libraries/System.Diagnostics.EventLog/tests/EventLogTests/EventLogTests.cs

index c9c90a4..3a14266 100644 (file)
@@ -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();
index 37ea855..e4b0f40 100644 (file)
@@ -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));
+        }
     }
 }