APGen Documentation Previous Topic: Script Error Events Next Topic: Pluggable Streams Parent Topic: APGen Developer's Guide    APGen Developer's Guide
Logging in APGen
See Also:

Since APG scripts are often run in the background, script errors are reported to a log file and/or the system Event Log.  This allows administrators to verify that scripts executed correctly, and to review script errors.

The Log Object

The Log object controls how information is logged.  The Log.Flags value specifies which information is sent to the log file and/or the Event Log.  Log.Filename, Log.Dir, and Log.Path contain information about the path of the log file.  The default log file is in the same directory that contains the script file, and is named "<Scriptname>.log".  The default log file directory can be changed when using programmatic execution by setting the APGen.LogDir value.

The log object is used to record syntax and runtime errors, as discussed in the Error Handling topic.  You can also use the log object to record any other information.

As discussed in the Error Handling topic, run-time errors are logged by default.  The following script (script.apg) causes an error.  The error is logged (whether Script.Debug is true or not).  Since there is no error handling code, the error halts script execution (unless the script is debugged):

<%#  Option Explicit

     Dim objFoo

     ' Will cause a run time error, since no Foo COM object exists
     Set objFoo = Script.CreateObject("Foo")

     ' Any further code won't be executed
#%>

Running this script causes the log file script.log to be created:

#Software: WebGecko Software Active Page Generator 2.1
#Version: 2.1
#Date: 1999-08-25 23:46:07
#Fields: time , severity , code , text
23:46:07 , Fatal , 0x800401f3 , APGen APGScript Object: Unable to find CLSID for ProgID: 'Foo' [C:\TEMP\apgs\script.apg Line: 6]      Set objFoo = Script.CreateObject("Foo")

In this example, we modify the script to use script language error handling.  When script language error handling is used, errors are intercepted and not reported to APGen, so they are not automatically logged.  So, the following example calls Log.Write() to report the error.

<%#  Option Explicit

     Dim objFoo

     ' Enable error handling
     On Error Resume Next

     Set objFoo = Script.CreateObject("Foo")
     If Err.number <> 0 Then
          ' Object couldn't be created

          ' Log error
          Log.Write "Couldn't create 'Foo' object", apgSeverityError
     Else
          ' Use objFoo
          ...
     End If

     ' Reset error handling
     On Error Goto 0

     ' Continue execution
     ...
#%>

Running this script causes the following entry to be added to the script.log file:

#Date: 1999-08-25 23:54:25
23:54:25 , Error , 0 , Couldn't create 'Foo' object

As shown in this example, Log.Write() can be used to explicitly log errors and other information.

Logging to the Event Log

The NT Event Log provides a standard, centralized way for applications (and the operating system) to record important software and hardware events.  Windows NT supplies the Event Viewer for viewing and filtering the Event Log, and a programming interface for examining the Log.  In addition, administrators can view Event Logs on remote computers, and many system management tools provide access to Event Logs.

APGen makes it easy to write entries to the NT Event Log.  It is recommended that only important events be logged to the Windows NT Event log, because the log can become full.  APGen leaves it up to each script author to determine which events are important.  For more information on logging to the NT Event Log, see Log.LogEvent().

The Log.Flags property can be set so that log entries with severity apgSeverityError and apgSeverityFatal are written to the system Event Log.  This code sets the log flags so that errors are logged to the Event log, and warnings and information are written to the log file:

Log.Flags = apgEventLogErrors Or apgLogWarnings Or apgLogInfo