How To Write to the Event Log from PowerBuilder

As a PowerBuilder developer you might have several log files from your application to log sequence of events and capture any exceptions or errors while processing. But if you have a batch application running on a server, you might be interested in logging your messages to the Event Viewer of the application server. This will provide a way for the DevOPs team to keep a tab on any failures and report the same instead of the developers going through the logs. This article explains How to write to the Event Log from PowerBuilder applications.

How To Write to the Event Log from PowerBuilder

The code is pretty simple and you can incorporate it very easily in your existing applications. You can follow the steps below to do the same.

Global External Functions:

Declare 3 external functions as below

Function ulong RegisterEventSource ( ulong lpUNCServerName, string lpSourceName ) Library “advapi32.dll” Alias For “RegisterEventSourceW”
Function boolean ReportEvent ( ulong hEventLog, uint wType, uint wCategory, ulong dwEventID, ulong lpUserSid, uint wNumStrings, ulong dwDataSize, string lpStrings[], ulong lpRawData ) Library “advapi32.dll” Alias For “ReportEventW”
Function boolean DeregisterEventSource ( ref ulong hEventLog ) Library “advapi32.dll”

Create a Global Function:

Name: f_write_event_log()


Integer: arg_messagelevel

String: arg_errmsg

String: arg_messagesource

Returns: None

//This function will log error/warning/information messages to the Event Log of the Application Server where the Application is running

integer li_messagelevel
string ls_errmsg[]
ulong lul_eventsource
//long ls_eventsource
//arg_messagelevel 4 = Information, 1 = Error, 2 = Warning

//// the event message is here
ls_errmsg[1] = arg_errmsg
//// setting the source of the message
//ls_messagesource = this.classname()

lul_eventsource = RegisterEventSource(0, arg_messagesource)

//IF IsNull(ls_eventsource) THEN ls_eventsource = -1

// set the level of the message
//li_messagelevel = 4 // Information, also 1 = Error, 2 = Warning

IF lul_eventsource > 0 THEN
// write to the log
ReportEvent(lul_eventsource, arg_messagelevel, 0, 0, 0, &
UpperBound(ls_errmsg), 0, ls_errmsg, 0)

Call f_write_event_log

You can inject your function call wherever you see necessary. For instance I have called the function whenever a DB Connect operation fails like below:

if sqlca.sqlcode <> 0 then
//Write to Event Log
f_write_event_log(1,”DB Connect Failed for LogID: ” + SQLCA.LogId + “. Error: ” + sqlca.sqlerrtext, <app name>)
end if

I hope this article helped you in adding some customized logging to the Event Log of your App Server. Do leave your comments below if any questions or suggestions.

p.s. Image Source: Pixabay

About pbgeeks 12 Articles
PBGeeks is a website for PowerBuilder Professionals to come together and explore the new features being released in PB. Participate in the Forum and exchange your thoughts with the other PB Gurus. Job seekers can find plenty of most recent jobs all around the world in the Jobs Listing section. Employers can post their job openings to be made available to the entire PB community.