The EventLog component reads and writes event logs and the eventlog component reads and writes logs.
The EventLog control in. Net allows you to access or customize Windows event logs, which record information about important software or hardware events. You can use EventLog to read existing logs, write items to logs, create or delete event sources, delete logs, and respond to log items. You can also create a log when creating an event source.
View Code // instantiate a Windows event log instance
EventLog log1 = new EventLog ();
Private void button10_Click (object sender, EventArgs e)
{
// Whether the event source exists
If (! EventLog. SourceExists ("TestLog "))
{
// Create an event Source, create an application, and use the specified Source as the daily
// Valid event source for the log entry written in the log, and the application is on the local computer. The source name used for p1 registration,
// The Name Of The log written by the p2 source entry
EventLog. CreateEventSource ("TestLog", "log1 ");
}
// Log name
Log1.Log = "log1 ";
// Event source name
Log1.Source = "TestLog ";
// Machine name
Log1.MachineName = ".";
// Write log information, specifying the category
Log1.WriteEntry ("An error has occured", EventLogEntryType. Error );
// Traverse the existing log information
Foreach (EventLogEntry item in log1.Entries)
{
Console. WriteLine (item. Message + "\ t" + item. TimeGenerated );
}
After execution, you can view the recorded log information in the computer management.