@@ -367,18 +367,28 @@ public static void CollectComputer(DataSet ds)
367367 // Addition - Adding Last system reboot time // Clintonw 9/8/2022 Issue #42
368368 // Based on Event 12 "Microsoft-Windows-Kernel-General"
369369 EventLog SystemEventLog = new EventLog ( "System" , "." ) ;
370- EventLogEntryCollection LogEntryCollection = SystemEventLog . Entries ;
371- int LogEntriesCount = LogEntryCollection . Count ;
372- for ( int i = LogEntriesCount - 1 ; i > - 1 ; i -- )
373- {
374- EventLogEntry LogEntry = LogEntryCollection [ i ] ;
375- if ( LogEntry . EntryType . ToString ( ) . Equals ( "Information" ) && LogEntry . Source == "Microsoft-Windows-Kernel-General" && LogEntry . InstanceId == 12 )
376- { // Console.WriteLine(LogEntry.Source + LogEntry.EntryType + LogEntry.Message + LogEntry.TimeGenerated);
377- Computer [ "LastSystemReboot" ] = LogEntry . TimeGenerated ; // Uptime: days, hours, min
378- break ;
370+ try
371+ {
372+ EventLogEntryCollection LogEntryCollection = SystemEventLog . Entries ;
373+ int LogEntriesCount = LogEntryCollection . Count ;
374+ for ( int i = LogEntriesCount - 1 ; i > - 1 ; i -- )
375+ {
376+ EventLogEntry LogEntry = LogEntryCollection [ i ] ;
377+ if ( LogEntry . EntryType . ToString ( ) . Equals ( "Information" ) && LogEntry . Source == "Microsoft-Windows-Kernel-General" && LogEntry . InstanceId == 12 )
378+ { // Console.WriteLine(LogEntry.Source + LogEntry.EntryType + LogEntry.Message + LogEntry.TimeGenerated);
379+ Computer [ "LastSystemReboot" ] = LogEntry . TimeGenerated ; // Uptime: days, hours, min
380+ break ;
381+ }
379382 }
380- }
381- SystemEventLog . Close ( ) ;
383+ }
384+ catch ( Exception ex )
385+ {
386+ Computer . LogException ( $@ "Failure to read last reboot information from the System event log.", ex ) ;
387+ }
388+ finally
389+ {
390+ if ( SystemEventLog != null ) SystemEventLog . Close ( ) ;
391+ }
382392
383393 }
384394
0 commit comments