EventLog class and Event Viewer

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Our app uses the EventLog class to write errors to the event log. Sometimes the app runs unattended for days, and if errors are occurring, the event log can fill up, causing the app to have no way of reporting further errors. The user needs to manually clear the event log in this case

To prevent this, we would like to specify the "Overwrite as needed" option on our event log to make it circular. This option is available as a checkbox on the properties page of the log itself when viewed through the event viewer

I don't see any methods on the EventLog class for setting this property. Is there a way to set this programatically

--thanks, Robb
 
Hi Robb,
You can achieve this by catching an exception of type
System.ComponentModel.Win32Exception

in the catch block you can then clear the event log.
Below is the code that will achieve this.

static void WriteToEventLog()

{

Console.WriteLine("Writing to the Event Log");

if(!EventLog.SourceExists("DotNetConsoleApp"))

{

EventLog.CreateEventSource("DotNetConsoleApp","Application");

}

try

{

EventLog el = new EventLog();

el.Source = "Application";

el.WriteEntry("Writing to event log",EventLogEntryType.Information);

}

catch(System.ComponentModel.Win32Exception ex)

{

if (ex.Message == "The event log file is full")

{

EventLog el = new EventLog();

el.Source = "Application";

el.Clear();

}

}

}

Regards
--
Deepak

#*#*#*#*#*#*#*#*#*#
I code therefore I am

rgilmore said:
Our app uses the EventLog class to write errors to the event log.
Sometimes the app runs unattended for days, and if errors are occurring, the
event log can fill up, causing the app to have no way of reporting further
errors. The user needs to manually clear the event log in this case.
To prevent this, we would like to specify the "Overwrite as needed" option
on our event log to make it circular. This option is available as a
checkbox on the properties page of the log itself when viewed through the
event viewer.
I don't see any methods on the EventLog class for setting this property.
Is there a way to set this programatically?
 
Thanks Deepak.

However that is not exactly what I was trying to achieve. I was hoping to mark the event log as "Over write as needed", as is done through the GUI checkbox.

I believe this causes the oldest errors to be overwritten first, rather than simply blowing the whole log away when it becomes full.

Any suggestions on how to do that?

thanks
Robb
 
Back
Top