Wednesday, July 14, 2021

Capturing infolog messages in D365FO using X++.

    Capturing info log messages in D365FO. We can get it in multiple ways.

    
  • Way 1.      
public static str getError()
{
    SysInfologEnumerator    sysInfologEnumerator;
    SysInfologMessageStruct infoMessageStruct;
    str                     logMessage;
    str                           logString;
    str                          ret;
    int                          i;
    #Define.NewLine('\n')

    sysInfologEnumerator = SysInfologEnumerator::newData(infolog.infologData());

    while (sysInfologEnumerator.moveNext())
    {
	i = 1;

	if (logMessage)
	{
	    logMessage += #Newline;
	}

	infoMessageStruct = SysInfologMessageStruct::construct(sysInfologEnumerator.currentMessage());

	while (i <= infoMessageStruct.prefixDepth())
	{
	    logString = logString + infoMessageStruct.preFixTextElement(i) + '. ';
	    i++;
	}

	logString = logString + infoMessageStruct.message();
	logMessage = logMessage + infoMessageStruct.message();
    }

    return logMessage;
}
      
  • Way 2
public void method()
{
    System.Exception        ex;
	
    try
    {
	
    }         
    catch
    {
        ex = CLRInterop::getLastException().GetBaseException();
	error(ex.get_Message());

        --- or----

        System.Exception e = ex;
	while (e != null)
	{
	    errorMessage += e.Message;
	    e = e.InnerException;
	}
    }
}


  • Way 3  (Get the type of exception using the below code.)
    container infologContents = infolog.cut(); 

    for (int i = 1; i <= conLen(infologContents); i++)
    {
	container infologLine = conPeek(infologContents, i);

	Exception errorLevel = conPeek(infologLine, 1);
	str logMessage = conPeek(infologLine, 2);
				
	if (logMessage)
	{
	    switch (errorLevel)
	    {
		case Exception::Error :
		    errors.add(logMessage);
		    break;

		case Exception::Warning :
		    warnings.add(logMessage);
		    break;

		case Exception::Info :
		    infos.add(logMessage);
		    break;

		default:
		    Debug::assert(false);
		    warnings.add(logMessage);
		    break;
	    }
        }
    }

  • Way 4
SysInfologEnumerator 	enumerator;
SysInfologMessageStruct msgStruct;
Exception 		exception;
str 			error;

enumerator = SysInfologEnumerator::newData(infolog.cut());

while (enumerator.moveNext())
{
    msgStruct = new SysInfologMessageStruct(enumerator.currentMessage());
    exception = enumerator.currentException();
    error = strfmt("%1 %2", error, msgStruct.message()) + "\n";
}

  • Way 5 (Return only error messages from info log)
    SysInfologEnumerator sysInfologEnumerator = SysInfologEnumerator::newData(infolog.infologData());
    while (sysInfologEnumerator.moveNext())     { switch (sysInfologEnumerator.currentException()) {     case Exception::Error:     retvalue = strFmt("%1", sysInfologEnumerator.currentMessage());     break; }     }

  • Way 6
    infolog.text(infologLine());   
  • Way 7
 	str errormessage;
	errormessage = infolog.text(infologLine());
	errormessage = infolog.text(infologLine()-1);
	errormessage = errormessage + " "+ infolog.text(infologLine());
	Error(errormessage);
    

Keep daxing!!

No comments:

Post a Comment