Software Development : A dizzy job...keeping abreast and being competitive is a 24X7 involvement
Jan 15, 2009
EventLog: Exploring Event Logs, Event Sources, and Entries
1. To create the custom log
// Source cannot already exist before creating the log.
if (System.Diagnostics.EventLog.SourceExists("Source1"))
{
System.Diagnostics.EventLog.DeleteEventSource("Source1");
}
// Logs and Sources are created as a pair. System.Diagnostics.EventLog.CreateEventSource("Source1", "NewLog1");
// Associate the EventLog component with the new log.
eventLog1.Log= "NewLog1"; eventLog1.Source = "Source1";
2. To delete a custom log
if (System.Diagnostics.EventLog.Exists("NewLog1"))
{
System.Diagnostics.EventLog.Delete("NewLog1");
}
3. To write entries to the log
eventLog1.WriteEntry("This is an informational message");
eventLog1.WriteEntry("This is an error message", System.Diagnostics.EventLogEntryType.Error);
4. To clear log entries
eventLog1.Clear();
5. To verify that the custom log exists
bool logExists = System.Diagnostics.EventLog.Exists("NewLog1");
MessageBox.Show("Does the log exist? " + logExists.ToString());
6. To verify that the source exists
bool sourceExists = System.Diagnostics.EventLog.SourceExists("Source1");
MessageBox.Show("Does the source exist? " + sourceExists.ToString());
7. To remove the event source you created
if (System.Diagnostics.EventLog.SourceExists("Source1"))
{
System.Diagnostics.EventLog.DeleteEventSource("Source1");
}
8. To read entries from the custom log you created
if(eventLog1.Entries.Count > 0)
{
foreach (System.Diagnostics.EventLogEntry entry in eventLog1.Entries)
{
MessageBox.Show(entry.Message);
}
}
{
MessageBox.Show("There are no entries in the log.");
}
Subscribe to:
Posts (Atom)