你想做什么样的日志?是写到数据库里还是要写到NT的日志里?

解决方案 »

  1.   

    演示用API和APP对象两种方法写NT的事件日志Using the Win32 API to write to the NT EventLog  
         
     Recently a tip went out that showed you how to write to the NT EventLog using the App object. This method has 2 limitations:1) You cannot use the code during a debug session.
    2) The source entry in the Event Log is always VBRuntime. Using the Win32 API alleviates these problems. Enter the following code in the General Declarations of a module:Declare Function RegisterEventSource Lib "advapi32.dll" Alias "RegisterEventSourceA" (ByVal lpUNCServerName As String, ByVal lpSourceName As String) As LongDeclare Function DeregisterEventSource Lib "advapi32.dll" (ByVal hEventLog As Long) As LongDeclare Function ReportEvent Lib "advapi32.dll" Alias "ReportEventA" (ByVal hEventLog As Long, ByVal wType As Integer, ByVal wCategory As Integer, ByVal dwEventID As Long, ByVal lpUserSid As Any, ByVal wNumStrings As Integer, ByVal dwDataSize As Long, plpStrings As Long, lpRawData As Any) As BooleanDeclare Function GetLastError Lib "kernel32" () As LongDeclare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As LongDeclare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long'-- Public Constants
    Public Const EVENTLOG_SUCCESS = 0
    Public Const EVENTLOG_ERROR_TYPE = 1
    Public Const EVENTLOG_WARNING_TYPE = 2
    Public Const EVENTLOG_INFORMATION_TYPE = 4
    Public Const EVENTLOG_AUDIT_SUCCESS = 8
    Public Const EVENTLOG_AUDIT_FAILURE = 10Public Function WriteToEventLog(sMessage As String, sSource As String, iLogType As Integer, vEventID As Integer) As BooleanDim bRC As Boolean
    Dim iNumStrings As Integer
    Dim hEventLog As Long
    Dim hMsgs As Long
    Dim cbStringSize As Long
    Dim iEventID As IntegerhEventLog = RegisterEventSource("", sSource)
    cbStringSize = Len(sMessage) + 1
    hMsgs = GlobalAlloc(&H40, cbStringSize)
    CopyMemory ByVal hMsgs, ByVal sMessage, cbStringSize
    iNumStrings = 1'-- ReportEvent returns 0 if failed,
    '-- Any other number indicates success
    If ReportEvent(hEventLog, iLogType, 0, iEventID, 0&, iNumStrings, cbStringSize, hMsgs, hMsgs) = 0 Then
    '-- Failed
     WriteToEventLog = False
    Else
    '-- Sucessful
     WriteToEventLog = True
    End IfCall GlobalFree(hMsgs)
    DeregisterEventSource (hEventLog)
    End FunctionAn example of how to write to the NT EventLog:Call WriteToEventLog("Warning, file exceeded recommended limit.", "Test App", EVENTLOG_WARNING_TYPE, 1003)===========================================================Writing to the Windows NT event log  
         
     Windows applications typically write to the NT event log to provide the user with useful information. In VB5/6, the App object now provides methods to make writing to the event log in Windows NT a snap:'-- Start Event Logging
    Call App.StartLogging("", vbLogToNT)'-- Log Events to NT
    Call App.LogEvent("Info", vbLogEventTypeInformation)
    Call App.LogEvent("Error", vbLogEventTypeError)
    Call App.LogEvent("Warning", vbLogEventTypeWarning) Be aware though, these functions will only work in the compiled EXE. They will be ignored in design mode. Check out the Microsoft knowledge base article Q161306 for more information.