By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,986 Members | 2,030 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,986 IT Pros & Developers. It's quick & easy.

EventLog class and Event Viewer

P: n/a
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
Jul 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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","App lication");

}

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" <an*******@discussions.microsoft.com> wrote in message
news:7F**********************************@microsof t.com...
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

Jul 21 '05 #2

P: n/a
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
Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.