web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Writing messages generated during execution to a file.

(0) ShareShare
ReportReport
Posted on by

Hello,

Please suggest how can i write all info, warning & error messages generated while running a class to text file  like log file.

I was thinking if all the messages are written to infolog , if that be the case  then i should extract them  from infolog & write to a file using IO, is my understanding correct?

I have the same question (0)
  • Mav Profile Picture
    on at

    Hello experts,

    Would really appreciate any response on this issue.

    Thanks

    Mav

  • Suggested answer
    Sergei Minozhenko Profile Picture
    23,093 on at

    Hi Mav,

    In AX2012 i used class AifInfoLog to get full infolog.

    I wrapped all logic part to try-catch statement to be sure that unhandled error will not cause any troubles. And after try-catch i read information from AifInfoLog class and stored it to the field, but you can do whatever you want.

  • Suggested answer
    Martin Dráb Profile Picture
    237,807 Most Valuable Professional on at

    AX 2012 already has the ability to save infolog messages to a trace file. See the documentation; I also covered some extra details in my blog post Instrumentation and tracing.

    If tracing doesn't work for you, you can implement custom logic in Info.add().

  • Mav Profile Picture
    on at

    @Sergei Minozhenkoi   wrote the following, however no file is getting generated i think the code flow is like 1>Found error 2>Go to catch block 3>Terminate the execution & hence the code written after catch block for reading infolog & writing to file is never executed. Please suggest. Shared below is my code.

    @Martin Thanks for your suggestion,  for now i m looking for really simple solution which would read all messages generated during my class execution & then write to a file, however really appreciate your response & will strive to move to this level of  Instrumentation and tracing after i complete my basic stuff.

    protected void run()
    {
    //Read logs & store in txt
       AifInfoLog  lAifInfoLog = new AifInfoLog();
       TextIo txIoRead, txIoWrite;
       FileIOPermission fioPermission;
       container containFromRead;
       int xx,iConLength;
       str sTempPath,sFileName = filepath,sOneRecord;
       ;
    //END Read logs & store in txt
    
    try
    {
      some business logic 
      throw Global::error(strFmt("Filetype %1 is not supported",fileType));
    }
    
    catch (Exception::Error)
    {
        throw Global::error(strfmt("Caught 'Exception::Error'.");
    }
    
    // BEGIN CODE FOR writing to a txt file
    sTempPath = filepath ;//WINAPI::getTempPath();
    fioPermission = new FileIOPermission (sTempPath   sFileName ,"RW");
    fioPermission.assert();
    if (WINAPI::fileExists(sFileName))
        {
            WINAPI::deleteFile(sTempPath   sFileName);
        }
    containFromRead =  lAifInfoLog.getInfoLogData();
    while (containFromRead)
        {
            sOneRecord = "";
            iConLength = conLen(containFromRead);
            // Loop through the token in the current record.
            for (xx=1; xx <= iConLength; xx  )
            {
                if (xx > 1) sOneRecord  = " ";
                sOneRecord  = conPeek(containFromRead ,xx);
            }
            info(sOneRecord);
    
            // Read the next record from the container.
            containFromRead = txIoRead.read();
        }
    CodeAccessPermission::revertAssert();
    info("Test INFO");
    
    // END  CODE FOR writing to a txt file
    
    } // END run method

  • Martin Dráb Profile Picture
    237,807 Most Valuable Professional on at

    You're right, your code won't be executed, because an exception is thrown before it's reached. You would have to call your logic from the catch block, before interrupting execution by throwing an exception.

    Note that that it assumes that the whole thing isn't inside a transaction. If it was, your catch clause would be completely ignored.

    By the way, you don't have to prefix error() with Global:: - just throw error() does the same thing.

  • Mav Profile Picture
    on at

    As only using error does not display it in infolog as per this article, i thought of prefixing it with global.

    docs.microsoft.com/.../exception-handling-with-try-and-catch-keywords

    Surely there is something wrong with my code apart from placement itself.

    Can you pls suggest where to place it & some sample code which would read details from infolog & write to txt file, please note the log file should be generated for each execution irrespective of whether there were errors or only info & warnings.

  • Martin Dráb Profile Picture
    237,807 Most Valuable Professional on at

    If you don't trust me that error() and Global::error() calls the same thing, put a breakpoint to error() method in Global class and then call error(). You'll see it for yourself. Even the link you mentioned confirms what I said - it reads: "In X++ code, the static methods on the Global class can be called without the Global:: prefix. For example, the Global::error method can be called simply as error("My message.");". Note that s a sad fact is that code examples in AX documentation are usually pretty bad. 

    If you want to read from infolog at the end of a process, instead of collection messages at they occur, you can use SysInfologEnumerator class (an example).

    As you see, there are multiple options, therefore you should think carefully about your requirements to be able to pick the most suitable one.

  • Mav Profile Picture
    on at

    @Martin I trust you , always have :-).

    Coming to my issue , so what i did is created a method writeMessage2File(FilePath _filepath) and wrote this code, i am calling this method in every try & catch , in catch calling it before throw error line.

    Objective of code is to collect all type of messages during execution & then write a file with all messages, warning, errors faced during execution.

    So using AifInfoLog.getInfoLogData() i am trying to collect all infolog data in a container & then using TextIo, writing the container contents to a txt file.

    Unfortunately It errors out at this line txIoWrite.write(conAllMessages);  Error message is TextIo object not initialized.

    protected void writeMessages2File(FilePath _filepath)
    {
       //Read logs & store in txt
       AifInfoLog  lAifInfoLog = new AifInfoLog();
       TextIo txIoRead, txIoWrite;
       FileIOPermission fioPermission;
       container conAllMessages;
       int xx,iConLength;
       str sTempPath,sFileName = _filepath,sOneRecord;
       ;
      //END Read logs & store in txt
      conAllMessages =  lAifInfoLog.getInfoLogData();
      //containFromRead  = Binary::constructFromContainer(containFromRead);
        
        sTempPath = _filepath ;//WINAPI::getTempPath();
        fioPermission = new FileIOPermission
        (sTempPath   sFileName ,"RW");
         fioPermission.assert();
         if (WINAPI::fileExists(sFileName))
            {
                WINAPI::deleteFile(sTempPath   sFileName);
            }
        
        // Open a test file for writing.
            // "W" mode overwrites existing content, or creates the file.
            txIoWrite = new TextIo( sTempPath   sFileName ,"W");
        
            // Write records to the file.
            txIoWrite.write("conAllMessages");
        
            // Close the test file.
            txIoWrite = null;
        
        CodeAccessPermission::revertAssert();
        info("Success");
    
    

  • Sergei Minozhenko Profile Picture
    23,093 on at

    Hi Mav,

    Check where your code is executed? Probably you expect it to be executed on the client, but the code is executed on the server.

  • Mav Profile Picture
    on at

    Hi

    Can you please suggest where do i need to check this & make changes.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 664 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 522 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 303 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans