3

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?

zSynopsis
  • 4,854
  • 21
  • 69
  • 106

7 Answers7

12

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
9

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.

Thomas Fonseca
  • 542
  • 4
  • 12
6

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".

3

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

Gishu
  • 134,492
  • 47
  • 225
  • 308
2

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).

Xetius
  • 44,755
  • 24
  • 88
  • 123
0

Have you tried following code?

log.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, log.MinimumRetentionDays);
Max
  • 12,622
  • 16
  • 73
  • 101
Johan
  • 753
  • 2
  • 11
  • 31
0

Start Event viewer Choose your event log and right click, hit 'Properties' Bump up the maximum log size in KB (this must be in multiples of 64 KB, but if you enter a number it will be corrected if this is not the case to be a multiple. A sound value in production could be 100 MB for example, so 100,000 for example could be inputted here.

Note, in production you probably do not want to overwrite events if the log still gets full. This is indicative of erroneous logging or an error which is frequently logged and occurs.

Event log properties

Tore Aurstad
  • 3,189
  • 1
  • 27
  • 22