I'm looking for a (fairly pain-free) means of adding some Windows Application Event-Log support to a small legacy Delphi 5 application. We just want it to log when it starts-up, shuts-down, fails to connect to a database etc.
Several of the solutions/components I've seen seem to suggest that we'll need to make a resource DLL which the Windows Event Log Viewer will link to when trying to read our 'entries'. While this doesn't seem too onerous, I guess it's something else to keep in mind if/when we further develop the application in future – we'll need to keep this DLL up to date.
At some point in the future we will want to turn the application into a service, probably written in D2007.
So can anyone recommend a suitable route for adding events to the event log in D5? I'm looking for specific 'we used this and it was okay' comments rather than a Google trawl (which I can do myself!) Free or paid-for, really don't mind – but something that I could migrate to D2007 in the future is important.
Best Answer
Summary: Writing to the Windows Event Log using Delphi
If you are writing a Windows Service and need to write to the local machine's Windows Event Log then you can call TService.LogMessage as mentioned here.
For any other type of applications you can use the SvcMgr.TEventLogger undocumented helper class for TService to write the the local machine's Windows Event Log as mentioned here, here and here.
You can also use the Windows API ReportEvent function as mentioned here and here.
I've created a simple class to make it easier, it is available on GitHub.
ReportEvent supports writing a log entry to either a local or remote machine's Event Log. For a remote example see John Kaster's EDN article.
Note that you would also have to create a message file and register your event source otherwise all your log messages will be starting with something like this:
1, For more information on how to create a message file see Finn Tolderlund's tutorial or Michael Hex's article or you can use an existing MC and RES file included in the GitHub project.
2, Embed the RES file into your application by including the MessageFile.res in your DPR file. Alternatively you can create a dll for the messages.
3, The once-off registration requires admin rights writing to the registry so it us usually done as part of your application's installation process.
If you have need Windows event logging and other logging requirements you can also use logging frameworks such as log4d and TraceTool
See here if you want to write to the Event Log window in the Delphi IDE.