// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.IO;
eventkey?.Close();
}
// now create EventLog objects that point to those logs
- EventLog[] logs = new EventLog[logNames.Length];
+ List<EventLog> logs = new List<EventLog>(logNames.Length);
for (int i = 0; i < logNames.Length; i++)
{
EventLog log = new EventLog(logNames[i], machineName);
- logs[i] = log;
+ SafeEventLogReadHandle handle = Interop.Advapi32.OpenEventLog(machineName, logNames[i]);
+
+ if (!handle.IsInvalid)
+ {
+ handle.Close();
+ logs.Add(log);
+ }
}
- return logs;
+ return logs.ToArray();
}
internal static RegistryKey GetEventLogRegKey(string machine, bool writable)
Assert.Contains("", eventlog.Entries.LastOrDefault()?.Message ?? "");
}
}
+
+ [ConditionalFact(typeof(Helpers), nameof(Helpers.SupportsEventLogs))]
+ [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
+ public void GetEventLogEntriesTest()
+ {
+ foreach (var eventLog in EventLog.GetEventLogs())
+ {
+ // Accessing eventlog properties should not throw.
+ Assert.True(eventLog.Entries.Count >= 0);
+ }
+ }
}
}