0

since I have migrated my code to .net 4.5, I get errors when I try to write eventLog with eventID > 65535

here is my code where iID is > 65535:

 System.Diagnostics.EventLog appLog = new System.Diagnostics.EventLog();
 appLog.Source = "my source";
 appLog.WriteEntry(sMsg, EventLogEntryType.Error, iID);

based on the definition the eventID is well a int32 so I don't understand why I get error there.

here the stack trace:

   at System.Diagnostics.EventLogInternal.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData)
   at System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type, Int32 eventID)
   at WSeProcFilesHandler.EventLog_AddEntry(String sMsg, Int32 iID) in d:\Liox\TFS\Eprocurement\Main\Dev\Eprocurement\Services\WAeProcFilesHandler\WSeProcFilesHandler.cs:line 567

any help will be welcomed. thanks

Denfer06
  • 25
  • 5

1 Answers1

1

Well, that's stated in msdn

ArgumentException

  • or - eventID is less than zero or greater than UInt16.MaxValue.

No explanation why an Int32 parameter is limited to an UInt16, to be honest...

A way to manage this would be to use eventlogs categories (see this overload). So you might have 65536 ids for each category.

By the way, that's a lot of distinct eventlog ids, no ?

Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122
  • it's what I saw but like you I don't understand why it's limited to UInt16.MaxValue. Is there any way to increase this size ? – Denfer06 Jan 08 '14 at 13:46
  • I will use a fix eventID and move the iID intto the sMsg string. thanks for your help – Denfer06 Jan 08 '14 at 14:22
  • @Denfer06 yes, the eventID should be something like a "type of event", not a sort of autoincremental number. – Raphaël Althaus Jan 08 '14 at 14:24
  • Hi thanks yes I'd followed a tutorial on creating a service and have a method called GetEventId() that was just incrementing. This just caught me out too (weird as this is an x64 app?!). Anyway, as you say, high amount of distinct IDs and I can just assign an ID for each module that calls to write an update to the log. – TilleyTech Jun 07 '19 at 07:46