Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Log it or lose it

Log events to the Windows NT Event Log with JNI

  • Print
  • Feedback

Page 3 of 4

Writing to the NT Event Log in Java

Now that we've covered the NT event logging basics, let's turn our attention to a Java application that writes event messages to the NT Event Log. First, you'll see a DLL that contains the message definitions in the message file. Next, you'll see a JNI DLL that logs events. A sample Java program logs the events into the Event Log by calling a function of the JNI DLL. The JNI DLL logs the events into the Event Log by referring to an event source that has already been registered. The details are described in the following sections:

  • Create message files
  • Create the JNI DLL
  • A sample program that writes to Event Log


Create message files

Message files are either .exe files, or DLLs with an associated message-string resource type. You build such resources with the MC.EXE message-compiler utility included in Microsoft Visual C++. A Win32 DLL project that contains the message file can be created in Visual C++. For our example, we created SampleAppMessages.dll in Visual C++ 6.0 for both messages and categories. It is a self-registering/deregistering DLL that can register and deregister the SampleApp event source into the Event Log using the regsvr32 utility.

Message definitions

SampleAppMessages.dll contains the SampleAppMessages.mc message file with the message and category format strings the sample Java program employs to log events in the Event Log. SampleAppMessages.mc includes the mc SampleAppMessages.mc custom-build command that produces the SampleAppMessages.rc resource file. Moreover, we've incorporated the SampleAppMessages.rc file into the Visual C++ project SampleAppMessages.dsp.

The message file SampleAppMessages.mc comprises two sections:

  • Header
  • Message format strings


You must define MessageIdTypedef=DWORD in the header section. The header section's other items, such as Language, Severity, and Facility, receive the default values of International English, Success, and Application, respectively. Therefore, you don't specify them in the header section.

The message-format string contains the definition of both category- and message-format strings to create a single DLL for both the event and category messages. The category-format strings resemble message-format strings, but they begin with a MessageID of 0x1 and do not possess any insertion strings. We have defined two categories for the SampleApp:

  • Data read
  • Data write


You'll find them defined in SampleAppMessages.mc as:

;define CAT_DATA_READ 1
MessageId=0x1
Language=English
Data Read
.
;define CAT_DATA_WRITE 2
MessageId=0x2
Language=English
Data Write
.


Notice that the symbolic names -- CAT_DATA_READ and CAT_DATA_WRITE -- double as comments. The MC compiler ignores these, but inserts them in the header file as #define for use in the application. For example, the compiler inserts define CAT_DATA_READ 1 in the header file as #define CAT_DATA_READ 1.

We further defined two messages as:

MessageId=0x1000
SymbolicName=SQLSTMT
Language=English
SQL Statement=%1
.
MessageId=0x1001
SymbolicName=ERROR
Language=English
The Error is=%1
.


The SQLSTMT and ERROR format strings contain one placeholder apiece; it is indicated by %1, which gets replaced by the insertion strings supplied to ReportEvent by the SampleApp for logging SQL statements and errors.

  • Print
  • Feedback

Resources