In this recipe, we will undertake the study of a simple example of generating our own log in the text format, which could be used in addition to the standard TestComplete log or even instead thereof.
For this purpose, we need to redefine the standard events.
In order to generate our own log we need to perform the following steps:
GeneralEvents_OnLogMessage
function GeneralEvents_OnLogMessage(Sender, LogParams) { var logFilePath = "c:\tclog.txt"; if(!aqFile.Exists(logFilePath)) { aqFile.Create(logFilePath); } var logFile = aqFile.OpenTextFile(logFilePath, aqFile.faReadWrite, aqFile.ctUTF8); logFile.WriteLine(aqDateTime.Now() + " MESSAGE: " + LogParams.Str); logFile.Close(); }
OnLogError
, OnLogEvent
, OnLogWarning
, and so on).Log.Message
, Log.Warning
, and all the other redefined handlers. For example:function testOwnLog() { Log.Message("Demo message"); Log.Warning("Demo warning"); }
As a result, in addition to the ordinarily generated log, we will have the following file created: C: clog.txt
, where each log message will be duplicated in the file.
First, we check if the file of the log exists, and then create it, if needed. Hereafter, we open it in "read-and-write" mode.
Into this file, we place the current date and time and the text of the message (which we obtain from the method of LogParams.Str
), after which we go ahead and close the file.
This is quite an inefficient method of working with the file, since for each of the messages we have to open and close it, which is telling on the performance; however, this example demonstrates the possibility of creating our own log with the help of intercepting logging events.
In a true-to-life project, we would need to think through a more advanced variant. For example, storing the messages into an array and saving the changes onto the disk, if the array reaches a certain amount of messages.