9/08/2012

Log method 1

"Creating log is a must." You can read those words in all specification. But who wants to redevelop this again and again. I made two different version for logging. In this discussion you will see how I do a log with the standard 'StdNotesLog' template.
First you must create a field in a profile document (or any similar solution), where you must defining where is the application's log nsf.
I have a log script library what I copying into the new application. I am going to show how I use it in an agent, but you can use it in the same way -for example- in a form.
In your next step you must "Use" the script library.
Use "log"
 Set the log db:
Call logdb.Openwithfailover(db.Server, profiledoc.logdb(0))
And you are ready to write into the log db:
Call logging( db.Server & "!!" & db.Filepath & " ~~ " & ag.Name & " agent started width rights of " & ag.Commonowner ) 
The logging method is waiting for a string. So you can do this too:
Call logging("Mail was send:" & Chr(13)_
            & dbltab & "From: " & maildoc.from(0) & Chr(13)_
            & dbltab & "To: " & Join(maildoc.SendTo , "; ") & Chr(13)_
            & dbltab & "Subject: " & maildoc.subject(0))
In this case there will be a multi line log entry. The dbltab is double tab (constant).
And here comes the  function:
%REM
    Function logging
    Created by Robert Takacs-Kral
%END REM
Function logging(eventstr As string)
    Dim eventItem As NotesItem
    If logview.AllEntries.Count = 1 Then Set logdoc = logview.GetFirstDocument() Else Set logdoc = logview.GetLastDocument()
   
    If Not logdoc Is Nothing Then
        If logdoc.Size > 30000 Then
            logdoc.FinishTime = Now()
            Call logdoc.Save(True, False)
            Set logdoc = Nothing
        End if
    End If
    If logdoc Is Nothing Then
        Set logdoc = logdb.Createdocument()
        logdoc.form = "Events"
        logdoc.server = logdb.server
        logdoc.StartTime = Now()
        Call logdoc.Save(True, False)
    End If
    Set eventItem = logdoc.getfirstitem("EventList")
    If eventItem Is Nothing Then
        Set eventItem = New NotesItem(logdoc, "EventList", Now() & " " & eventstr)
    Else
        Call eventItem.AppendToTextList( Now() & " " & eventstr)
    End If
    Call logdoc.Save(True, False)
   
End Function
This short code writes into the last log document, but when the size is near 32K close the current document and creates a new one.
This is a simple code but works well.

No comments:

Post a Comment