tags:

views:

1809

answers:

4

Hi all,

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?

+4  A: 

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
I think he may expect a means to empty it programatically, not manually via the event viewer
Vinko Vrsalovic
Another solution is to increase the capacity of the event log.
Jeff Sternal
@Vinko: Given the "Does anyone know why this is happening" I'm not sure... definitely needs more information.
Jon Skeet
A: 

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
+1  A: 

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
+2  A: 

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

Scott Vercuski