views:

309

answers:

2

I want to see about logging events from a Delphi 5 application to the Windows log, and from another post here I see that I can use the TEventLogger class to do this. However, I can't find any documentation on the syntax of the TEventLogger.LogMessage procedure, so I don't know what all the parameters mean, how to use them, or even what possible values are available. I've tried looking around, and all I find is a page from Embarcadero stating that the function exists, but nothing on its syntax, and MSDN is no help as I can only find the BizTalk version which does me no good. Does anyone have a help page or information on this that might shed some light on what I can do with it? Thanks.

+4  A: 

TEventLogger is an internal helper class for TService. You log message using the TService.LogMessage() function, not by calling into TEventLogger directly. The parameters of LogMessage() directly match with the parameters of the Win32 API ReportEvent() function. Look in the Win32 API documentation for details.

If you are not writing a service application, then you need to call the Win32 API RegisterEventSource() and ReportEvent() functions directly instead.

Remy Lebeau - TeamB
This one'll be fun. I'm eventually going to need both methods as the application can run as a service (which we want to make it into) or a standalone executable (which it currently is). At least I know how to detect which mode it's running in and can switch appropriately. Thanks for the help everyone.
Tom
+5  A: 

A simple example of an application writing to the event log:

procedure WriteToLog(Msg:string; EventId: Word = 0);
var
  h: THandle;
begin
  h := RegisterEventSource(nil, PChar(Application.ExeName));
  if h > 0 then
  try
    ReportEvent(h, 0, 0, EventId, nil, 1, 0, @Msg, nil);
  finally
    DeregisterEventSource(h);
  end;
end;

procedure TForm7.Button1Click(Sender: TObject);
begin
  WriteToLog('* Blah Blah Blah *');
end;

But beware that not registering the EventID with the system will give this kind of confused Description:

The description for Event ID ( 0 ) in Source ( C:\Documents and Settings\fgaillard\My Documents\RAD Studio\Projects\Project1.exe ) cannot be found. The local computer may not have the necessary registry information or message DLL files to display messages from a remote computer. You may be able to use the /AUXSOURCE= flag to retrieve this description; see Help and Support for details. The following information is part of the event: * Blah Blah Blah *.

François