I’ve two processes A and B.
Process A keeps on writing EventLogEntry every 5 secs.
Process B should listen to EntryWritten event on the EventLog Object and ASAP should report on screen that an entry has been written.
How to create Process B. (exe) that should be running always until closed manually.
Pls see the following code snippet:
class Observer
{
static void Main(string[] args)
{
EventLog log = new EventLog("logname", "MyMachine", "source");
log.EntryWritten += new EntryWrittenEventHandler(log_EntryWritten);
log.EnableRaisingEvents = true;
// The thread shouldn't exit here but wait till the event is received
// When received, should call the handler
// and then again keep waiting for next event.
}
static void log_EntryWritten(object sender, EntryWrittenEventArgs e)
{
if (e.Entry.Source == "source")
{
Console.WriteLine("Message " + e.Entry.Message);
Console.WriteLine("InstanceId " + e.Entry.InstanceId);
Console.WriteLine("Source " + e.Entry.Source);
Console.WriteLine("TimeWritten " + e.Entry.TimeWritten);
Console.ReadLine();
Console.WriteLine("\n");
}
}
}
How can it be done?
Thanks.
You should remove the call to
Console.ReadLinein thelog_EntryWrittenhandler. Otherwise you will block the handler.To avoid that your program terminates immediately you have to add this code at the end of
Main:Your main thead will block until a key is pressed on the console. The thread used by the
EventLogclass to service theEventWrittenevent will be used to execute your handler every time a new event arrives. Except you have to study this quote from Event.EventWritten Event that has some information that about your 5 seconds requirement: