Search code examples
genexus

Creating log functionalities with Genexus


It's my first time with Genexus and I want to know how to create log functionalities.

I created a transaction object "Logs" but I don't need any user interface interaction. Also, I want to insert record to the "Logs" table directly inside events like "After Trn".

How can I accomplish that? What's the best approach?

I'll appreciate any help. Thanks!


Solution

  • You need to define a procedure with the information to be logged, for example logAdd

    If it is a web application you can extract the user from the websession, if it is a win application you need to pass it on parms.

    Then you call the procedure on BeforeComplete of every transaction.

    Web Example:

    logAdd.Call(&Pgmname, "Clients", CliId.ToString(), &Mode) on BeforeComplete;
    

    It is important that the logAdd procedure has the Commit on exit property in false. This way the information will be logged only if the transaction is confirmed.

    Update - getting old values in your logging procedure

    To get the previous values of a transaction you can take advantage of bussiness components. After setting your transaction as a Bussiness Component you put the following rules on your transaction:

    [WEB] {
        &Clients.load(CliId) if update on BeforeValidate;
        logClients.Call(CliId, &Clients, &Mode) on BeforeComplete;
    }
    

    &Clients is a variable based on your Bussiness Component type.

    In prc:logClients you can access all the old values through &Clients.Att.GetOldValue(), and the new values through a regular For Each