Another post for .NET developers
Sometimes your .NET app needs to write something to Windows' "event log" - an error message or just an informational entry etc. etc. The "quick and dirty" way to do that is:
EventLog.WriteEntry("Application", "TEST", EventLogEntryType.Warning);
This will write an entry to the "Application" log, under the "Application" source. But the problem with that is you end up with this huge useless message:
The description for Event ID 0 from source Application cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer. If the event originated on another computer, the display information had to be saved with the event. The following information was included with the event: TEST the message resource is present but the message is not found in the string/message table
"TEST" being the only useful info there.
That is because the proper way to write to the event log is to create your own "event source" then provide a "message file" that holds event-id descriptions for your app, etc etc blah-blah.
This is a PITA, a headache AND this requires admin rights on the target machine which is not always the case, expecially if you're running an ASP.NET app in a sandboxed environment.
To solve this error I just need to find another event "source" which fits the following criteria:
After looking through the registry key there's a perfect candidate: ".NET Runtime"
This key is present on any machine that has .NET Framework installed and messages are defined in
mscoree.dll. We just need to find a proper message-id for us to pass to our
WriteEntry call. Let's have a look inside the DLL:
OK, it seems message numbers are starting from 1000 (0x3E8) and all of them just output the passed info using the "%1" substitute. Cool. "1000" it is.
OK, so the quick and dirty way of writing to the log would be:
var message = "Critical error, we're all gonna die"; EventLog.WriteEntry(".NET Runtime", message, EventLogEntryType.Warning, 1000);
The event log message is now nice and clean.
Alex has founded Jitbit in 2005 and is a software engineer passionate about customer support.