How to write to the Application log in Windows NT, Windows 2000, or Windows XP using the Windows Script Host (257541)



The information in this article applies to:

  • Microsoft Visual FoxPro for Windows 3.0
  • Microsoft Visual FoxPro for Windows 3.0b
  • Microsoft Visual FoxPro for Windows 5.0
  • Microsoft Visual FoxPro for Windows 5.0a
  • Microsoft Visual FoxPro for Windows 6.0
  • Microsoft Visual FoxPro for Windows 7.0
  • Microsoft Visual FoxPro 8.0
  • Microsoft Visual FoxPro 9.0 Professional Edition

This article was previously published under Q257541

SUMMARY

The Microsoft Script Host makes it easy to write to the application event log of Microsoft Windows NT 4.0, Windows 2000, Windows XP, or later versions of Windows. The Event Viewer is used to view and to manage logs of system, program, and security events on computers that are running Windows NT 4.0 or later versions of Windows. You can use the event logs in Event Viewer to gather information about hardware, software, and system problems, and to monitor Windows security events.

MORE INFORMATION

Windows records events in three kinds of logs in Windows NT 4.0, Windows 2000, Windows XP, and later versions of Windows:
  • Application log
    The application log contains events logged by applications or programs. For example, a database program might record a file error in the application log. The developer decides which events to record.
  • System log
    The system log contains events logged by the Windows NT 4.0, Windows 2000, and Windows XP and later system components. For example, the failure of a driver or other system component to load during startup is recorded in the system log. The event types logged by system components are predetermined.
  • Security log
    The security log can record security events such as valid and invalid logon attempts, as well as events related to resource use, such as creating, opening, or deleting files. An administrator can specify what events are recorded in the security log. For example, if you have enabled logon auditing, attempts to log on to the system are recorded in the security log.
Event Viewer displays these types of events:
  • Error
    A significant problem, such as loss of data or loss of functionality. For example, if a service fails to load during startup, an error is logged.
  • Warning
    An event that is not necessarily significant, but might indicate a possible future problem. For example, when disk space is low, a warning is logged.
  • Information
    An event that describes the successful operation of an application, driver, or service. For example, when a network driver loads successfully, an information event is logged.
  • Success audit
    An audited security access attempt that succeeds. For example, a user's successful attempt to log on to the system is logged as a success audit event.
  • Failure audit
    An audited security access attempt that fails. For example, if a user tries to access a network drive and fails, the attempt is logged as a failure audit event.
The event log service starts automatically when you start Windows NT 4.0, Windows 2000, Windows XP, and later versions of Windows. Application and system logs can be viewed by all users, but security logs are accessible only to administrators.

The following code demonstrates how to use the Windows Script Host to write to the application log in Windows NT 4.0, Windows 2000, or later versions of Windows. If you are using this code on Windows 95 or Windows 98, it will write to Wsh.log in the user's Windows directory. The Wsh.log file contains a time stamp, the event type, and the text of the log entry.

The LogEvent method uses the following syntax:

LogEvent(intType, strMessage)
					

where:

intType is a number representing the type of the event.
strMessage is the text of the log entry.

 

#DEFINE SUCCESS 0
#DEFINE ERROR 1
#DEFINE WARNING 2
#DEFINE INFORMATION 4
#DEFINE AUDIT_SUCCESS 8
#DEFINE AUDIT_FAILURE 16
WshShell = CreateObject("WScript.Shell")
*!* Logevent returns .t. for SUCCESS for .f. for FAILURE
?WshShell.LogEvent(SUCCESS, "Logon Script Completed Successfully")
WshShell.LogEvent(ERROR, "Logon Script Completed Successfully")
WshShell.LogEvent(WARNING, "We are getting low on disk space!!")
WshShell.LogEvent(INFORMATION, "Start nightly backup")
WshShell.LogEvent(AUDIT_SUCCESS, "Checksum Success!!")
WshShell.LogEvent(AUDIT_FAILURE, "Checksum Failure")

WshShell=.NULL.
				

REFERENCES

For additional information, visit the following Microsoft Developer Network (MSDN) Web site:

Microsoft Windows Script technologies
http://www.msdn.Microsoft.com/scripting/


Modification Type:MajorLast Reviewed:3/11/2005
Keywords:kbCodeSnippet kbhowto kbMiscTools KB257541 kbAudDeveloper