MessageLog / LogMessages / LogMessage Example

 

picture

 

This example, during the Form_Load, connects to the default server, and blanks the Text boxes. Entering a date/time into Text1 and hitting the Command1 button retrieves at most 20 messages starting from the entered date/time and moving forward. The code for the click event for Command 1 first checks that a valid date/time has been entered. Future times and times earlier than 1/1/98 are rejected. The code calls MessageLog.List to retrieve the messages and does a For Each to walk through the returned LogMessages collection. A string is built from the properties of the each returned LogMessage and added to the ListBox List1

When Command2 is hit the code in the Click event checks that some string has been entered in Text2 as the message and then calls MessageLog.PutString to send the message to the log.

 

Build the MessageLog example as follows:

1. Create a new project and add a reference to the PISDK library.

2. Add the visual elements as shown in the picture. Make sure the numbered element names (label1, list1, &ldots;) match those shown.

3. Cut and paste the code below into the project.

 

Option Explicit
Dim srv As Server

Private Sub Command1_Click()
On Error GoTo handler
  If Text1.Text = "" Then
    MsgBox "Please enter a starting date/time from " & _
      "which to retrieve the next 20 messages"
    Exit Sub
  End If
  Dim dtStart As Date
  Dim dtMin As Date
  dtMin = CDate("1/1/98")
  dtStart = CDate(Text1.Text)
  If dtStart > Now() Then
    MsgBox "Please enter a date in the past"
    Exit Sub
  End If
  If dtStart < dtMin Then
    MsgBox "Please enter a start date after 1/1/98"
    Exit Sub
  End If
  Dim lgMsgs As LogMessages
  On Error Resume Next
  Set lgMsgs = srv.MessageLog.List(dtStart, _
      -1#, 20, "*", "*", -1)
  If Err.Number <> 0 Then
    If Err.Number = pseNOMESSAGESFOUND Then
      MsgBox "No messages for this time period."
      Exit Sub
    Else
      MsgBox Err.Description
      Exit Sub
    End If
  End If
  On Error GoTo handler
  Dim lgMsg As LogMessage
  Dim msgString As String
  List1.Clear
  For Each lgMsg In lgMsgs
    msgString = lgMsg.LogTime
    msgString = msgString + " | " + lgMsg.ProgramName
    msgString = msgString + " | " + lgMsg.message
    List1.AddItem msgString
  Next
Exit Sub
handler:
  MsgBox Err.Description
End Sub

Private Sub Command2_Click()
On Error GoTo handler
  If Text2.Text = "" Then
    MsgBox "Please enter some text to send as a message."
    Exit Sub
  End If
  srv.MessageLog.PutString Text2.Text
  MsgBox "Message logged"
Exit Sub
handler:
  MsgBox Err.Description
End Sub

Private Sub Form_Load()
Set srv = Servers.DefaultServer
Text1.Text = ""
Text2.Text = ""
End Sub

Enabling Operational Intelligence