I keep getting this error message when trying to write to the Event log from a console app.
Here's how i write to it
public static void WriteToEventLog(Exception ex)
{
string mySource = "Export Task";
if (!EventLog.SourceExists(mySource))
EventLog.CreateEventSource(mySource, "Application");
EventLog myLog = new EventLog();
myLog.Source = mySource;
myLog.WriteEntry(ex.ToString());
}
Does anyone know why this is happening and how i can fix it?
To me it sounds like:
- It's happening because the event log is full.
- Fix it by emptying the event log.
If this isn't the case, please edit your question to make it clear that you believe the event log isn't full, and how you came to this conclusion.
I had the same error on Windows XP using Visual C# 2010 Express and this is what worked for me:
Go to Start->Control Panel->Administrative Tools->Event Viewer
Then select Action->Properties and select "Overwrite Events as Needed"
You could also increase the maximum size of the log.
If you open the event viewer, right click on the event log in question and select "properties" you can see the event log size. You can make it larger or change the options below it to say "Overwrite events as needed".
Most likely scenario, you're using the default Application log which is usually written to by almost any app on your machine (just like the one you wrote). Over time the event log fills up and reaches its maximum
So my suggestion would be to create a new custom event log instead of reusing the "Application" log. That way you can catch this exception (which is a valid case) and handle it by calling EventLog.Clear & retrying the write event operation. Also check if you're flooding the event log, log responsibly.
On the other hand, you could also change the event log properties for the Application log. (Event Viewer, Bring up properties on the Application log node) - You can specify the max size of the log (512 KB) as well as how to handle wrapping e.g. Overwrite entries older than 7 days when the max size is reached. (This seems to be the default on my WinXP machine). But you'd have to do this on every machine... but its something that you could try
Custom event log sources have a default size of 512k. This has bit me several times. You can override this to make it a much more sane size (especially if you are writing a log of information for debug purposes).
Have you tried following code?
log.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, log.MinimumRetentionDays);