IN THIS TASK
- Troubleshooting Notes
- Using Windows Scripting Host Shell to Log Events
- Using a Visual Basic Component to Log Events
- Testing Event Logging
NOTE: If you want to append some text to an Internet Information Server Log, you can use the Response.AppendToLog method.
- 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.
- 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\ApplicationNOTE: 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.
- Set the following Registry key to 0 instead of 1, and then restart your computer for the changes to take effect.
Using Windows Scripting Host Shell to Log EventsThe 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 MethodThis 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
- From the Windows Start menu, click Run, and then type notepad to open Notepad.
- Highlight the following code, right-click the code, and then click Copy. In Notepad, click Paste on the Edit menu.
<%@ Language=VBScript %>
'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
set WshShell = Server.CreateObject("WScript.Shell")
wshshell.Logevent WARNING, "Test Event Log by Windows Script Host!"
Response.write "Event Logged Successfully by Windows Script Host!"
- On the File menu, click Save.
- 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 EventsAnother 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 MethodThis 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
- 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.
- In the New Project dialog box, click ActiveX DLL, and then click Open.
- From the Project menu, click Project Properties, and then select the Unattended Execution and Retained in Memory check boxes. Click OK.
- 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.
- In Project Explorer, click Class1, highlight the current name, and then rename Class1
- 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
- Save the MyEventLog project to the folder of your choice.
- On File menu, click Make MyEventLog.dll.
- 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.
Creating an ASP Page to Use the EventLog Object
- From the Start menu, click Run, type notepad, and then click OK to open Notepad.
- 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 %>
Const vbLogEventTypeError = 1 'Error.
Const vbLogEventTypeWarning = 2 'Warning.
Const vbLogEventTypeInformation = 4 'Information.
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
- On the File menu, click Save.
- 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 LoggingTo test event logging, follow these steps:
- Open your browser. If Internet Explorer is installed on your computer, from the Start menu, click Run, type iexplore, and then click OK.
- 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.aspTo 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
- 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.
- 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
- 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:
- 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.
ID articol: 301309 - Ultima examinare: 6 aug. 2012 - Revizie: 1