How To Log Events from Active Server Pages

Exonerare de răspundere pentru conținutul retras din baza de cunoștințe

Acest articol a fost scris despre produsele pentru care Microsoft nu mai oferă asistență. Prin urmare, acest articol este oferit „ca atare” și nu mai va fi actualizat.

IMPORTANT: This article contains information about modifying the registry. Before you modify the registry, make sure to back it up and make sure that you understand how to restore the registry if a problem occurs. For information about how to back up, restore, and edit the registry, click the following article number to view the article in the Microsoft Knowledge Base:
256986 Description of the Microsoft Windows Registry

IN THIS TASK

Summary

This article demonstrates two methods of writing events to an Event Log from Active Server Pages (ASP). ASP does not provide a built-in mechanism to log events to Microsoft Windows Event Logs. One of the popular approaches is to build custom Component Object Model (COM) components using Microsoft Visual Basic or Microsoft Visual C++, which provide methods to write events to an Event Log and use the component in the ASP pages. Another approach is to use the Shell object of Windows Script Host.

NOTE: If you want to append some text to an Internet Information Server Log, you can use the Response.AppendToLog method.

Requirements

  • Microsoft Windows 95, Windows 98, Windows NT 4.0 Server, Windows NT 4.0 Workstation, Windows 2000 Professional, Windows 2000 Server, or Windows 2000 Advanced Server
  • Microsoft Internet Information Server (IIS) or Personal Web Server (PWS)

    To install IIS 5.0 on computers that are running Windows 2000, from the Windows Start menu, point to Settings, point to Control Panel, and then click Add/Remove Programs. Click Add/Remove Windows Components.

    On computers that are running Windows NT, you can install IIS 4.0 or PWS from Windows NT 4.0 Option Pack.

    On computers that are running Windows 95 or Windows 98, you can install PWS from the Windows 95 CD or Windows 98 CD.

Troubleshooting Notes

  • Event logging does not work in components that you call from ASP on Windows 2000.

    By default, components that are run from an ASP page are created under the IUSR_COMPUTERNAME account. This account is a member of the Guests group, and the security privileges that are needed to write the application event log changed in Windows 2000. To resolve this problem, you must modify a registry key. For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
    251264 App.Logevent does not work in components that you call from ASP on Windows 2000

  • You may receive a "Permission denied" error message when you call the LogEvent method.

    This error message occurs because, by default, the Application Event Log is set to restrict guest access, and the Anonymous user is part of the Guest group. To work around this problem, use any of the following methods:WARNING: If you use Registry Editor incorrectly, you may cause serious problems that may require you to reinstall your operating system. Microsoft cannot guarantee that you can solve problems that result from using Registry Editor incorrectly. Use Registry Editor at your own risk.

    • Set the following Registry key to 0 instead of 1, and then restart your computer for the changes to take effect.
      HKLM\System\CurrentControlSet\Services\EventLog\Application
      Name: RestrictGuestAccess
      Type: REG_DWORD
      NOTE: This enables all Guest accounts to write to the Application Event Log.
    • Remove the Anonymous user from the Guest group.
    • Use a different Anonymous user for this specific page.
    • Turn off Anonymous access for the page or application.

Using Windows Scripting Host Shell to Log Events

The easiest way to log events from ASP pages is to use Windows Scripting Host Shell. You can make use of the LogEvent method of the Shell object to write events directly to the event logs.

Requirements That Are Specific to This Method

 This method requires that you install Microsoft Windows Script Host. You can download and install Windows Script 5.7 from the following Microsoft Web sites: 

Windows Script 5.6 for Windows XP and Windows 2000

Creating an ASP Page to Log Events

  1. From the Windows Start menu, click Run, and then type notepad to open Notepad.
  2. Highlight the following code, right-click the code, and then click Copy. In Notepad, click Paste on the Edit menu.
    <%@ Language=VBScript %>
    <HTML>
    <BODY>
    <%
    'Use these Constants to designate the type of Event Log.
    const SUCCESS = 0
    const ERROR = 1
    const WARNING = 2
    const INFORMATION = 4
    const AUDIT_SUCCESS = 8
    const AUDIT_FAILURE = 16

    dim WshShell
    set WshShell = Server.CreateObject("WScript.Shell")
    wshshell.Logevent WARNING, "Test Event Log by Windows Script Host!"
    set wshshell=nothing
    Response.write "Event Logged Successfully by Windows Script Host!"

    %>
    </BODY>
    </HTML>
  3. On the File menu, click Save.
  4. In the Save As dialog box, click the down arrow in the Save In text box, and click the root of your PWS or IIS Web server (which is C:\InetPub\Wwwroot by default). In the Save As Type drop-down list box, click All Files. In the File Name text box, type EventLog.asp. Finally, click Save.

Using a Visual Basic Component to Log Events

Another way to log the events to the Event Log is to create a Visual Basic component that uses the LogEvent method of the Application object to write the events to the Event Log. In addition to this method, you can use the LogPath and LogMode properties to specify the path to a custom log file and determine how logging will be carried out. If no LogPath is set, the LogEvent method writes to the Windows NT LogEvent file.

Requirements That Are Specific to This Method

This method requires that you install Visual Basic 6.0. In this method, you create a Visual Basic ActiveX DLL project in order to create a COM component that you can use in your ASP page. This COM component contains a method that uses the LogEvent method of the Application object to write the passed text to the Event Log.

You can download the latest version of Microsoft Visual Studio, which contains Visual Basic, from the following Microsoft Web site:

Creating a Visual Basic ActiveX DLL Project

  1. From the Windows Start menu, point to Programs, point to Microsoft Visual Studio 6.0, and then click Microsoft Visual Basic 6.0 to open Visual Basic.
  2. In the New Project dialog box, click ActiveX DLL, and then click Open.
  3. From the Project menu, click Project Properties, and then select the Unattended Execution and Retained in Memory check boxes. Click OK.
  4. In Project Explorer, select the project, and then press the F4 key to display the properties for the project. Highlight the current name, and then rename the project MyEventLog.
  5. In Project Explorer, click Class1, highlight the current name, and then rename Class1
    EventLog.
  6. Highlight the following code, right-click the code, and then click Copy. In Project Explorer, double-click EventLog, and then press the CTRL+V key combination to paste the following code in the code editor:
    Function LogEvent(strMessage As String, EventType As Integer) As Boolean
    App.LogEvent strMessage, EventType
    End Function
  7. Save the MyEventLog project to the folder of your choice.
  8. On File menu, click Make MyEventLog.dll.
  9. In the Make Project dialog box, do not change the name of the DLL, and click OK. The DLL is created in the Visual Basic Project folder with the same name as your project.
You must now create an ASP page that uses the newly created ActiveX DLL to write text to the Event Log.

Creating an ASP Page to Use the EventLog Object

  1. From the Start menu, click Run, type notepad, and then click OK to open Notepad.
  2. Highlight the following code, right-click the code, and then click Copy. In Notepad, click Paste on the Edit menu to paste the following code:
    <%@ Language=VBScript %>
    <HTML>
    <BODY>
    <%
    Const vbLogEventTypeError = 1 'Error.
    Const vbLogEventTypeWarning = 2 'Warning.
    Const vbLogEventTypeInformation = 4 'Information.

    dim myLog
    set myLog = server.CreateObject("MyEventLog.EventLog")
    myLog.LogEvent "Test Event Log by Visual Basic", vbLogEventTypeWarning
    Response.Write "Event Logged Successfully using Visual Basic Component!"
    set myLog = nothing

    %>
    </BODY>
    </HTML>
  3. On the File menu, click Save.
  4. In the Save As dialog box, click the root of your PWS or IIS Web server (which is C:\InetPub\Wwwroot by default). In the Save As Type drop-down list box, click All Files. In the File Name text box, type EventLogVB.asp. Finally, click Save.

Testing Event Logging

To test event logging, follow these steps:
  1. Open your browser. If Internet Explorer is installed on your computer, from the Start menu, click Run, type iexplore, and then click OK.
  2. To test the Event Log page that uses Windows Script Host, type the following address in the Address bar, and then press the ENTER key:
    http://<YourComputerName>/EventLog.asp
    To test the Event Log page that uses a Visual Basic component, type the following address in the Address bar, and then press the ENTER key:
    http://<YourComputerName>/EventLogVB.asp
  3. If you can see the messages that you typed in your ASP pages, you can open the event log viewer or event log files to see if the event has been logged.

Where Is My Event Log?

  • On Windows 2000-based or Windows NT-based systems, you can use the Event Viewer to read event logs. To open Event Viewer, from the Start menu, point to Programs, point to Administrative Tools, and then click Event Viewer. Click Application Log to see if the test event has been logged.
  • If you use Windows Scripting Host to log events on Windows 95-based, Windows-98 based, and Windows Me-based computers, events are logged to the WSH.log file, which is located in the users Windows directory. WSH.log contains a time stamp, the event type, and the text of the log entry.
  • If you use a Visual Basic component to log events on Windows 95-based, Windows 98-based, and Windows Me-based computer, the events are logged to the Vbevents file if no files are specified in the App.LogPath property of the Visual Basic Application object.

Troubleshooting

  1. App.LogEvent only logs in compiled applications.

    By design, App.LogEvent does not work when you run the application from Visual Basic Integrated Design Environment (IDE); it only works when your application is in a compiled state. For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
    161306 App.LogEvent only logs in compiled applications

  2. You must give the appropriate file permissions for the DLL.

    For Web use, you must give file permissions for the DLL to the following accounts:

    • IUSR_ComputerName
    • IWAM_ComputerName
    • System
    • Administrators Group

    NOTE: If you are not using Anonymous access to the Web site, you must give the Everyone group Read and Execute permissions for the DLL and the /WinNT/System32 directory.

References

For additional information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
154576 How to write to the Windows NT event log from Visual Basic

257541 How to write to the Windows NT/Windows 2000 system log by using the Windows Script Host

Proprietăți

ID articol: 301309 - Ultima examinare: 6 aug. 2012 - Revizie: 1

Feedback