Generating log in our own format

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.

How to do it...

In order to generate our own log we need to perform the following steps:

  1. Add the element Events to your project, if it has not been added yet (right-click on the name of the project and navigate to Add | New Item | Events).
  2. Double-click on Events in the General events element.
  3. In the right part of the window, unfold the element of General events and select the OnLogMessage element.
    How to do it...
  4. Click on the button New inside the OnLogMessage element.
  5. In the opened New Event Handler window, select the module in which the new handler will be located, and click on the OK button.
  6. In the result, within the selected module, there appears the following function:
    GeneralEvents_OnLogMessage
  7. Write the following code for the function:
    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();
    }
  8. Similarly, create other handlers for all the types of messages which you would like to have outputted into the external log-file (OnLogError, OnLogEvent, OnLogWarning, and so on).
  9. Write and launch a simple function, which will call the next methods 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.

How it works...

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.

There's more...

If you would like to block output of the information into a standard TestComplete log on top of duplicating the messages into a separate file, it is enough to write the following line in the beginning of the handler:

LogParams.Locked = true;
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.114.19