Microsoft KB Archive/161306

= INFO: App.LogEvent Only Logs in Compiled Applications =

Article ID: 161306

Article Last Modified on 5/13/2003

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 5.0 Control Creation Edition

-



This article was previously published under Q161306



SUMMARY
In Microsoft Visual Basic, Event Logging allows an application to add information to either a user-defined file or the Microsoft Windows NT or Windows 2000 Event Log when a user-defined criteria is met.

This functionality is only available when running a compiled application. If you attempt to use the logging methods while in the Microsoft Visual Basic development environment, the logging methods will be ignored.



MORE INFORMATION
The logging can be enabled for the development process by creating and compiling a user-defined ActiveX DLL or EXE component that exposes the logging methods of the App object. The application in the development environment need only to add a reference to this compiled component. Calling the exposed methods of this ActiveX component will allow logging to function. Below is a sample ActiveX DLL that exposes the StartLogging and LogEvent methods of the App object.

WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this code "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

 Start Microsoft Visual Basic. Open a new ActiveX DLL project. This will add Class1 to the new project. Change the Name property of Class1 to "Logging" (without quotes). From the Project menu, click Project1 Properties. Change the Name of the Project to "AppMessages" (without quotes).  Add the following code to the Logging class: Public Enum eLogModes mLogAuto = 0 mLogOff = 1 mLogToFile = 2 mLogToNT = 3 mLogOverwrite = &H10 mLogThreadID = &H20 End Enum

Public Sub StartLog(logTarget As String, logMode As eLogModes) App.StartLogging logTarget, logMode

End Sub

Public Sub AddLogEvent(LogBuffer As String, EventType As _     LogEventTypeConstants) App.LogEvent LogBuffer, EventType End Sub



NOTE: Other methods may be exposed to apply to your specific needs. See the Microsoft Visual Basic Online Help for the entire list of event log methods of the App object.

 From the File menu, click Make AppMessage.DLL. Select a folder in which to place the DLL, and click OK.</li> From the File menu, click New project. Click Yes to Save the AppMessage source code for later use.</li> Select Standard EXE. Click OK.</li> From the Project menu, click References.</li> Turn on the reference to "AppMessages." Click OK.</li>  To Form1, add the following code: Private Sub Form_Load Dim t As New AppMessages.Logging t.StartLog "C:\TEMP\Test.log", mLogToFile t.AddLogEvent "Form Loaded", vbLogEventTypeInformation Set t = Nothing End Sub

</li> From the Run menu, click Start. A new file "Test.log" will be created and placed into the C:\TEMP folder. This file will contain the information:

"Information Application C:\TEMP\Test.log: Thread ID: -316429 ,Logged: Form Loaded"</li></ol>

<div class="references_section">