AX2012/D365FnO – CAPTURING INFOLOG MESSAGES IN D365FNO USING X++.

D365fno-PostImage

Capturing Infolog Messages in D365FO Using X++

In Microsoft Dynamics 365 Finance and Operations (D365FO), the Infolog plays a crucial role in displaying system messages such as errors, warnings, and informational notes. As developers, there are times when we need to capture these messages programmatically for logging, debugging, or passing them back to external systems.

In this blog, I’ll walk you through how to retrieve Infolog messages using X++, explain the code snippet provided, and highlight the benefits of this approach.

Why Capture Infolog Messages?

Capturing Infolog messages can be extremely useful in scenarios such as:
 
Error Handling: Store error messages in custom logs or tables for later analysis.
Integration: Pass system messages to external applications or APIs.
Debugging: Track what messages were generated during a process for troubleshooting.
User Experience: Provide consolidated feedback to users in a structured format.
 

The X++ Code to Capture Infolog Messages

public static str getInfologMessages(int fromLine)
{
    str                     ret = '', currentMsg;
    int                     toLine, msgStart;
    container               infoLogData;
    SysInfologEnumerator    sysInfologEnumerator;
    
    // It records the current position (line number) of the InfoLog before your business logic runs
    toLine  = infologLine();

    if (toLine > fromLine)
    {
        // Delete the data in the InfoLog from the given line, so that we can add them
        // as part of error structure.
        infoLogData = infolog.cut(fromLine + 1, Global::infologLine());
        
        if(infoLogData)
        {
            sysInfologEnumerator = SysInfologEnumerator::newData(infoLogData);

            while(sysInfologEnumerator.moveNext())
            {
                currentMsg = sysInfologEnumerator.currentMessage();

                // trim the tab and spaces from the beginning
                msgStart = strNFind(currentMsg, '\t', 1, strLen(currentMsg));
                currentMsg = msgStart? subStr(currentMsg, msgStart, strLen(currentMsg)): currentMsg;

                ret += currentMsg + '\n';
            }
        }
    }

    return ret;
}

Breaking Down the Code

  • Parameters:
    • fromLine: The starting line number in the InfoLog from which we want to capture messages.
    • toLine: The last line number in the InfoLog.
  • Key Steps:
    1. Get the current InfoLog line count using infologLine().
    2. Cut messages from the InfoLog starting at fromLine + 1 using infolog.cut(). This ensures we only capture new messages generated after a certain point.
    3. Iterate through messages using SysInfologEnumerator.
      This class helps us loop through each message in the InfoLog container.
    4. Clean up formatting by trimming tabs/spaces from the beginning of each message.
    5. Concatenate messages into a single string (ret) separated by line breaks.
  • Return Value:
    A string containing all captured InfoLog messages.

Benefits of This Approach

  • Structured Logging: Easily store messages in custom tables or logs.
  • Integration Ready: Pass messages to external systems (e.g., APIs, middleware).
  • Improved Debugging: Developers can track exactly what messages were generated during execution.
  • User-Friendly Feedback: Consolidate multiple InfoLog messages into a single readable output.

Where This Can Be Helpful

  • Batch Jobs: Capture and store errors/warnings generated during batch processing.
  • Custom Services: When exposing D365FO functionality via services, return InfoLog messages to the calling system.
  • Testing & Debugging: Developers can quickly check what messages were raised during execution without relying solely on the UI.
  • Audit Trails: Maintain a history of system messages for compliance or troubleshooting.

Final Thoughts
Capturing InfoLog messages programmatically in D365FO using X++ is a powerful technique that enhances error handling, debugging, and integration capabilities. The provided method is a reusable utility that can be plugged into various scenarios where structured message handling is required.

This site uses cookies to offer you a better browsing experience. By browsing this website, you agree to our use of cookies.