In Windows 2008 server, when I clear the EventLog, I stop getting subsequent EntryWritten events. The code to see this is very simple:
private void eventLog1_EntryWritten(object sender, System.Diagnostics.EntryWrittenEventArgs e)
{
label1.Text = "Got entry at " + DateTime.Now;
}
private void write_Click(object sender, EventArgs e)
{
eventLog1.WriteEntry("Hello");
}
private void clear_Click(object sender, EventArgs e)
{
eventLog1.Clear();
}
this.eventLog1.EnableRaisingEvents = true;
this.eventLog1.Log = "MY_LOG";
this.eventLog1.Source = "Test";
this.eventLog1.SynchronizingObject = this;
this.eventLog1.EntryWritten += new System.Diagnostics.EntryWrittenEventHandler(this.eventLog1_EntryWritten);
In Windows 2003 server, the code works as expected. Does anyone have an idea why this happens, or how to make it work?
Microsoft confirmed this is a bug in Windows 7 and 2008 Server.
The workaround is to create a new EventLog object, and add an entry to the log before setting EnableRaisingEvents to true.
Hopefully this will be fixed...