Wednesday 5 March 2014

Clean up your databaselog

A cool thing in AX is to keep track of vital changes using the DatabaseLog. A problem however is that your databaselog can grow exponentially so there are two things you need when using it:
1. Choose carefully which data you want to keep track of. Mostly it's enough to track some updates on some fields and some delete actions. You should periodically review if it is still necessary to log these changes. 
2. You need to clean up your log for it can grow enormously within time. Disadvantage of the clean up functionality in AX is that it simply deletes your log. 

Therefor I created a job which exports your log (with the old and the new value) to a csv file which you can save and afterwards delete the log in AX. The job is just to show what could be possible to save your databaselog. You could improve it with date ranges, delete records after saving them, create a class which can run in batch, only a number of records to keep the file size normal and then on the next file, etcetera. 

static void JLH_DatabaseLogToCSV(Args _args)
{
    #AviFiles
    SysOperationProgress    progress = new SysOperationProgress();
    int                     i;
    AsciiIo                 asciiIo;
    Filename                filename;
    Str                     line;
    Container               con;
    SysDatabaseLog          databaseLog;
    str                     dataBaseLogOld;
    str                     dataBaseLogNew;
    str                     dataBaseLogDate;
    str                     dataBaseLogUser;
    str                     tableName;
    str                     fieldName;
    container               databaseLogContainer;
    int                     containerLength;
    int                     counter;
    int                     countRecords;
    int                     tableId;
    int                     fieldId;
    ;

    progress.setCaption("Export To CSV in progress...");
    progress.setAnimation(#AviTransfer);

    filename = "C:\\FileShare\\DAX2009\\ImportExport\\ExportToCSV.txt";
    asciiIo = new AsciiIo(filename,'W');
    asciiIo.outRecordDelimiter('\r\n');
    asciiIo.outFieldDelimiter(";");
    asciiIo.write('Table;Field;NewValue;OldValue;Date;User');

    while select dataBaseLog
    {
        progress.setText(strfmt("DataBaseLog Recid %1", databaselog.RecId));

        containerLength = conLen(dataBaseLog.Data);

        for (counter = 1; counter <= containerLength; counter++)
        {
            dataBaseLogContainer    = conPeek(databaseLog.Data, counter);
            dataBaseLogOld          = conpeek(databaselogContainer,3);
            dataBaseLogOld          = strKeep(dataBaseLogOld,' 1234567890,.abcdefghijklmnopqrstuvwxyz');
            dataBaseLogNew          = conpeek(databaselogContainer,2);
            dataBaseLogNew          = strKeep(dataBaseLogNew,' 1234567890,.abcdefghijklmnopqrstuvwxyz');
            dataBaseLogDate         = dateTime2str(databaseLog.createdDateTime);
            dataBaseLogUser         = databaseLog.createdBy;
            tableName               = tableid2name(databaseLog.table);

            tableID                 = databaseLog.table;
            fieldId                 = conpeek(databaseLogContainer,1);
            fieldName               = fieldid2name(tableID,fieldId);

            if (fieldName != 'RecVersion')
            {
                asciiIo.write(strfmt('%1;%2;%3;%4;%5;%6',
                tableName,
                fieldName,
                dataBaseLogNew,
                dataBaseLogOld,
                dataBaseLogDate,
                dataBaseLogUser));
            }
        }
    }
    asciiIo = null;
}