Writing to the Event Log From OpenInsight

This article contains a sample BASIC+ routine for use in OI (OpenInsight) that a developer can use to log information to the Windows Event Log on the system running OpenInsight. Writing to the event log is useful for web applications that need to log server exceptions that a system administrator can review later but it's also useful for desktop applications that need to do the same thing without bothering the user.

Writing to the event log might not be efficient or convenient when dealing with hundreds of log entries over long running processes but it is useful for important log entries that you want noticed by IT staff. When users report system problems the administrator is likely to review the event log for clues. The administrator may not be familiar with the location of custom log files or know how to access log entries saved inside the database so writing messages to the event log will make them more likely to be found.

The function below can be copied into your OpenInsight system as a store procedure and then called to write an event log message with an ERROR, WARNING, or INFO log type.

Function CS_WriteEventLog(Msg, Type)

$Insert Logical

common /cs_welCom/ cs_welInit@, cs_welObjHand@, cs_welUnused3@, cs_welUnused4@, cs_welUnused5@

*Either init the WSCript object or use the existing object
*if previously initialized.
isInit = If Assigned(cs_welInit@) Then cs_welInit@ Else False$
If isInit EQ True$ Then
	WScript = cs_welObjHand@
End Else
	WScript = oleCreateInstance("WScript.Shell")
	cs_welObjHand@ = WScript
	cs_welInit@ = True$
End

*Default to INFORMATION event log types.
Type = If Assigned(Type) Then Type Else "INFO"

*Reference http://technet.microsoft.com/en-us/library/ee176682.aspx
Begin Case
	Case Type _EQC "ERROR"
		TypeVal = 1
	Case Type _EQC "WARNING"
		TypeVal = 2
	Case Type _EQC "INFO"
		TypeVal = 4
	Case OTHERWISE$
		TypeVal = 4 ;*Default to informational
End Case

*Write a simple status message to Application event log.
ret = OleCallMethod(WScript, "LogEvent", TypeVal, Msg)

Return

For example, executing this function will write 'Hello World' to the application event log with an information log type:

CS_WriteEventLog("Hello World!", "INFO")

Happy Logging!

Share this post

Comments (2)

  • anon

    Hi Jared,

    Great piece!

    One question. I copied the code into a multi-user (ServerOnly=1) system and when I ran it from the server the event was logged in that server's event log. When I ran it from a PC on the network (shortcut to OI on that same server) the event was written to the PC's event log.

    How can we get it to log the event on the server instead?

    Thanks!!

    Apr 29, 2015
  • Jared Bratu's picture

    The code is intended to log to the event log on the system where oinsight.exe or oengine.exe is running. For a workstation to log to a remote computer's event log could be difficult and require opening up more access to the server than a user should be required to have. It might be better to interface OpenInsight with a centralized logging system or write the logs temporarily to a holding table and then have a scheduled task on the server poll and flush them to the Windows event log at the server. One last idea, you could continue write to the workstations event log and then query that workstation's event log from the server for matching events. This is pretty easy to do with Powershell in Windows 7 / 2008 and greater environments.

    Jul 23, 2016

Leave a comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.

About Us

Congruity Service is a technology solutions company bringing the best technology solutions to OpenInsight projects, Drupal sites, servers, networks, and your technology needs.