public void CopyingEventLogEntryCollection() { string log = "CopyCollection"; string source = "Source_" + nameof(CopyingEventLogEntryCollection); try { EventLog.CreateEventSource(source, log); using (EventLog eventLog = new EventLog()) { eventLog.Source = source; eventLog.WriteEntry(message); eventLog.WriteEntry("Further Testing"); EventLogEntryCollection entryCollection = eventLog.Entries; EventLogEntry[] entryCollectionCopied = new EventLogEntry[entryCollection.Count]; entryCollection.CopyTo(entryCollectionCopied, 0); int i = 0; foreach (EventLogEntry entry in entryCollection) { Assert.Equal(entry.Message, entryCollectionCopied[i].Message); i += 1; } } } finally { EventLog.DeleteEventSource(source); EventLog.Delete(log); } }
private static void PrintStartStop(EventLog systemLog) { EventLogEntryCollection entries = systemLog.Entries; EventLogEntry[] entriesArray = new EventLogEntry[entries.Count]; entries.CopyTo(entriesArray, 0); DateTime startTime = entriesArray.First().TimeGenerated; bool started = true; bool sleeping = false; foreach (EventLogEntry entry in entriesArray) { switch (GetEventType(entry)) { case EventType.StartingUp: if (started) { Program.PrintDate(startTime, entry.TimeGenerated, "unexpected restart"); } startTime = entry.TimeGenerated; started = true; break; case EventType.ShuttingDown: Program.PrintDate(startTime, entry.TimeGenerated, "shutting down"); started = false; break; case EventType.EnteringSleep: sleeping = true; Program.PrintDate(startTime, entry.TimeGenerated, "going to sleep"); started = false; break; case EventType.ResumingFromSleep: sleeping = false; startTime = entry.TimeGenerated; started = true; break; case EventType.SystemTimeChanged: if (sleeping) { sleeping = false; startTime = entry.TimeGenerated; } break; } } Program.PrintDate(startTime, DateTime.Now, "still running"); }
static void Main(string[] args) { try { string myLogName = "MyNewLog"; if (!EventLog.SourceExists("MySource")) { EventLog.CreateEventSource("MySource", myLogName); Console.WriteLine("Creating EventSource"); } else { myLogName = EventLog.LogNameFromSourceName("MySource", "."); } EventLog myEventLog2 = new EventLog(); myEventLog2.Source = "MySource"; myEventLog2.WriteEntry("Successfully created a new Entry in the Log"); myEventLog2.Close(); EventLog myEventLog1 = new EventLog(); myEventLog1.Log = myLogName; //Obtain the Log Entries of "MyNewLog" EventLogEntryCollection myEventLogEntryCollection = myEventLog1.Entries; myEventLog1.Close(); Console.WriteLine("The number of entries in 'MyNewLog' = " + myEventLogEntryCollection.Count); //Display the 'Message' property of EventLogEntry. for (int i = 0; i < myEventLogEntryCollection.Count; i++) { Console.WriteLine("The Message of the EventLog is :" + myEventLogEntryCollection[i].Message); } //Copy the EventLog entries to Array of type EventLogEntry. EventLogEntry[] myEventLogEntryArray = new EventLogEntry[myEventLogEntryCollection.Count]; myEventLogEntryCollection.CopyTo(myEventLogEntryArray, 0); IEnumerator myEnumerator = myEventLogEntryArray.GetEnumerator(); while (myEnumerator.MoveNext()) { EventLogEntry myEventLogEntry = (EventLogEntry)myEnumerator.Current; Console.WriteLine("The LocalTime the Event is generated is " + myEventLogEntry.TimeGenerated); } } catch (Exception e) { Console.WriteLine("Exception:{0}", e.Message); } }
private ObservableCollection <Event> GetEvents(EventLogEntryCollection entries) { var events = new ObservableCollection <Event>(); EventLogEntry[] entriesArray = new EventLogEntry[entries.Count]; entries.CopyTo(entriesArray, 0); var logs = entriesArray.OrderByDescending(e => e.TimeGenerated).Take(20).ToList(); for (var i = 0; i < logs.Count; i++) { events.Add(new Event { EventSource = logs[i].Source, DateAndTime = logs[i].TimeGenerated, Level = logs[i].EntryType.ToString(), Message = logs[i].Message.ToString() }); } return(events); }
public static void Main() { try { string myLogName = "MyNewLog"; // Check if the source exists. if (!EventLog.SourceExists("MySource")) { // Create the source. // An event log source should not be created and immediately used. // There is a latency time to enable the source, it should be created // prior to executing the application that uses the source. // Execute this sample a second time to use the new source. EventLog.CreateEventSource("MySource", myLogName); Console.WriteLine("Creating EventSource"); Console.WriteLine("Exiting, execute the application a second time to use the source."); // The source is created. Exit the application to allow it to be registered. return; } else { // Get the EventLog associated if the source exists. myLogName = EventLog.LogNameFromSourceName("MySource", "."); } // Create an EventLog instance and assign its source. EventLog myEventLog2 = new EventLog(); myEventLog2.Source = "MySource"; // Write an informational entry to the event log. myEventLog2.WriteEntry("Successfully created a new Entry in the Log"); myEventLog2.Close(); // Create a new EventLog object. EventLog myEventLog1 = new EventLog(); myEventLog1.Log = myLogName; // Obtain the Log Entries of "MyNewLog". EventLogEntryCollection myEventLogEntryCollection = myEventLog1.Entries; myEventLog1.Close(); Console.WriteLine("The number of entries in 'MyNewLog' = " + myEventLogEntryCollection.Count); // Display the 'Message' property of EventLogEntry. for (int i = 0; i < myEventLogEntryCollection.Count; i++) { Console.WriteLine("The Message of the EventLog is :" + myEventLogEntryCollection[i].Message); } // <Snippet2> // Copy the EventLog entries to Array of type EventLogEntry. EventLogEntry[] myEventLogEntryArray = new EventLogEntry[myEventLogEntryCollection.Count]; myEventLogEntryCollection.CopyTo(myEventLogEntryArray, 0); IEnumerator myEnumerator = myEventLogEntryArray.GetEnumerator(); while (myEnumerator.MoveNext()) { EventLogEntry myEventLogEntry = (EventLogEntry)myEnumerator.Current; Console.WriteLine("The LocalTime the Event is generated is " + myEventLogEntry.TimeGenerated); } // </Snippet2> } catch (Exception e) { Console.WriteLine("Exception:{0}", e.Message); } }
//通过系统日志获取开关机时间等 #pragma warning disable CS0618 // entry.EventID 类型或成员已过时 public static Dictionary <string, string> GetUpTime() { Dictionary <string, string> timedic = new Dictionary <string, string>(); EventLog eventlog = new EventLog(); //"Application"应用程序, "Security"安全, "System"系统 eventlog.Log = "System"; EventLogEntryCollection eventLogEntryCollection = eventlog.Entries; //中间的事件太多了,不需要,只需要最近的开机关机事件 EventLogEntry[] logEntry = new EventLogEntry[eventLogEntryCollection.Count]; eventLogEntryCollection.CopyTo(logEntry, 0); //最后一次开机时间 DateTime lastPowerOn = DateTime.Now; //最后一次关机时间 DateTime lastPowerOff = DateTime.Now; //上次关机时长 double lastDownTime = 0; //已运行时间 double upTime = 0; bool onstatus = false; bool offstatus = false; for (int i = logEntry.Length - 1; i >= 0; i--) { if (logEntry[i].EventID == 6005 && !onstatus) { lastPowerOn = logEntry[i].TimeGenerated; timedic.Add("lastPowerOn", lastPowerOn.ToString()); onstatus = true; } else if (logEntry[i].EventID == 6006 && !offstatus) { lastPowerOff = logEntry[i].TimeGenerated; timedic.Add("lastPowerOff", lastPowerOff.ToString()); offstatus = true; } if (onstatus && offstatus) { //Console.WriteLine("poweron:{0} poweroff:{1}", lastPowerOn, lastPowerOff); break; } } lastDownTime = Math.Ceiling(ExecDateDiff(lastPowerOn, lastPowerOff)); upTime = Math.Ceiling(ExecDateDiff(DateTime.Now, lastPowerOn)); timedic.Add("lastDownTime", lastDownTime.ToString()); timedic.Add("upTime", upTime.ToString()); //Console.WriteLine(upTime.ToString()); //Console.WriteLine(lastDownTime.ToString()); //首次开机时间 DateTime firsetPowerOn = DateTime.Now; //首次关机时间 DateTime firsetPowerOff = DateTime.Now; //总运行时间 double totalUpTime = 0; bool first_onstatus = false; bool first_offstatus = false; foreach (EventLogEntry entry in eventLogEntryCollection) { if (entry.EventID == 6005 && !first_onstatus)//开机 { firsetPowerOn = entry.TimeGenerated; timedic.Add("firsetPowerOn", firsetPowerOn.ToString()); first_onstatus = true; } else if (entry.EventID == 6006 && !first_offstatus)//关机 { firsetPowerOff = entry.TimeGenerated; timedic.Add("firsetPowerOff", firsetPowerOff.ToString()); first_offstatus = true; } if (first_onstatus && first_offstatus) { //Console.WriteLine("poweron:{0} poweroff:{1}", firsetPowerOn, firsetPowerOff); break; } } totalUpTime = Math.Ceiling(ExecDateDiff(DateTime.Now, firsetPowerOn)); timedic.Add("totalUpTime", totalUpTime.ToString()); //Console.WriteLine(totalUpTime); return(timedic); }
/// <summary> /// Mains this instance. /// </summary> public static void Main() { try { // Check if the source exists. if (!EventLog.SourceExists("MySource")) { // Create the source. // An event log source should not be created and immediately used. // There is a latency time to enable the source, it should be created // prior to executing the application that uses the source. EventLog.CreateEventSource("MySource", "MySource"); Console.WriteLine("Creating EventSource"); Console.WriteLine("Exiting, execute the application a second time to use the source."); Thread.Sleep(2000); // The source is created. sleep to allow it to be registered. } string myLogName = EventLog.LogNameFromSourceName("MySource", "."); // Create a new EventLog object. EventLog myEventLog1 = new EventLog(); myEventLog1.Log = myLogName; //Create test messages. myEventLog1.Clear(); for (int i = 0; i < 20; i++) { CreateNewLogMessage("MySource", "The entry number is " + i); } // Obtain the Log Entries of "MyNewLog". EventLogEntryCollection myEventLogEntryCollection = myEventLog1.Entries; //myEventLog1.Close(); Console.WriteLine("The number of entries in 'MyNewLog' = " + myEventLogEntryCollection.Count); // Copy the EventLog entries to Array of type EventLogEntry. EventLogEntry[] myEventLogEntryArray = new EventLogEntry[myEventLogEntryCollection.Count]; myEventLogEntryCollection.CopyTo(myEventLogEntryArray, 0); Console.WriteLine("before deleting"); // Display the 'Message' property of EventLogEntry. PrintAll(myEventLogEntryCollection); myEventLog1.Clear(); Console.WriteLine("process deleting"); foreach (EventLogEntry myEventLogEntry in myEventLogEntryArray) { //Console.WriteLine("The LocalTime the Event is generated is " // + myEventLogEntry.TimeGenerated + " ; " + myEventLogEntry.Message); if (myEventLogEntry.Index % 2 == 0) { CreateNewLogMessage("MySource", myEventLogEntry.Message); } } Console.WriteLine("after deleting"); PrintAll(myEventLogEntryCollection); myEventLog1.Close(); } catch (Exception e) { Console.WriteLine("Exception:{0}", e.Message); } Console.ReadKey(); }