tags:

views:

137

answers:

2

When writing to the event log, our application uses enums to generate an error number to attach to event log items. The idea is to list the things that we know can go wrong, and assign IDs to each one so we can identify right away what went wrong when viewing the event log.

One enum contains values/IDs that represent a class in the application, and another enum contains values representing error codes for known errors that may occur during execution (e.g. invalid date=1, invalid amount=2 and so on).

Now, say we've detected an invalid input to a method. We add up the appropriate class ID that contains the method from one enum, and the specific "invalid input" error from the second enum, and then we pass the result of the addition to the EventLog.WriteEntry () method along with the message string.

The problem is that when we pass a variable containing the result of the enum values' addition to the WriteEntry() method, nothing gets written to the event log. However, if the value is passed as a regular integer in the method's parameters, then the event is written successfully.

Does anyone have any idea why this is happening?

A: 

why would you use enums for those in any case?

Wouldn't it make much more sense to use extensions to Exceptions?

e.g. throw new InvalidArgumentException("error message")

Jonathan Fingland
+1  A: 

Put two calls to your event log into your code, e.g. something like:

EventLog.WriteEntry(1005);
EventLog.WriteEntry(MethodIDs.FirstMethod + ErrorIDs.FifthError);

Put a breakpoint on the first line, run the program, and then single-step through both calls to the function, watching what happens.

You will probably find the problem within a few seconds.

Jason Williams
The 1st call would write to the event log just fine, but the 2nd one would do nothing.
Jason Williams