#1 2014-09-19 11:27:00

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,534
Website

Working with windows logs (tutorial)

Since many of us use mORMot as a service I decided to provide small tutorial how to say Windows to format log messages correctly during Windows log output.
In simplest case to put event to Windows log we do something like this pseudo code:

const EventSourceName = 'myApplicationName';
var FEventLog: Integer = 0;
 
function toWinLog(const aMessage: string)
var 
  P: pointer; 
  ID: DWord;
begin
  P := PChar(aMessage); ID := 0;
  if FEventLog = 0 then
    FEventLog := RegisterEventSource(nil, PChar(EventSourceName));
  ReportEvent(FEventLog, EVENTLOG_INFORMATION_TYPE, 0, 0, nil, 1, ID, @P, nil);
end;

If we look to windows log after call to toWinLog('OUR MESSAGE') we can see something like:

The description for Event ID 0 from source myApplicationName 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: 
  OUR MESSAGE

The first 6 lines of message  added by winlog because he is do not know what is event with ID = 0.
To sad possible event ID's we must add special resource file to our application which describe possible event ID's, if output format, parameters, localization and so on.
The simplest way is to provide only one ID = 0 for language English is:
1. Create file myApplicationMessages.mc

SeverityNames=(Success=0x0:       STATUS_SEVERITY_SUCCESS
               Informational=0x1: STATUS_SEVERITY_INFORMATIONAL
               Warning=0x2:       STATUS_SEVERITY_WARNING
               Error=0x3:         STATUS_SEVERITY_ERROR
              )

FacilityNames=(System=0x0:  FACILITY_SYSTEM
               Runtime=0x2: FACILITY_RUNTIME
               Stubs=0x3:   FACILITY_STUBS
               Io=0x4:      FACILITY_IO_ERROR_CODE
              )

LanguageNames=(English=0x409:MSG00409)

MessageIdTypedef=WORD

MessageId=0x1
Severity=Success
Facility=Application
SymbolicName=CATEGORY_SUCCESS
Language=English
%1
.

MessageId=0x2
Severity=Success
Facility=Application
SymbolicName=CATEGORY_INFORMATION
Language=English
%1
.

MessageId=0x3
Severity=Success
Facility=Application
SymbolicName=CATEGORY_WARNING
Language=English
%1
.

MessageId=0x4
Severity=Success
Facility=Application
SymbolicName=CATEGORY_ERROR
Language=English
%1
.

2. Compile it using MC.exe utility from Windows SDK. In my case

> "C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\mc.exe" myApplicationMessages.mc

This command produce file MSG00409.bin  (409 is a language ID)

3. Create myApplicationMessages.rc file: 

LANGUAGE 0x9,0x1
1 11 "MSG00409.bin"

3. Compile it using brcc

4. Include compiled res file to your application dpr
{$R 'myApplicationMessages.res' 'myApplicationMessages.rc'}

Now toWinLog('OUR MESSAGE') produce in Windows log Application section:

  OUR MESSAGE

Hope it help for somebody...

Offline

#2 2014-09-19 12:29:59

ComingNine
Member
Registered: 2010-07-29
Posts: 294

Re: Working with windows logs (tutorial)

Thank you for your knowledgeable tutorial and kind sharing !

Offline

#3 2019-05-04 04:14:25

ComingNine
Member
Registered: 2010-07-29
Posts: 294

Re: Working with windows logs (tutorial)

mpv wrote:

Since many of us use mORMot as a service I decided to provide small tutorial how to say Windows to format log messages correctly during Windows log output
...
Hope it help for somebody...

Can you help to inform how to work with linux logs, i.e., append to /var/log/messages or /var/log/the_subject_app ? Many thanks !

Offline

Board footer

Powered by FluxBB