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  (It fill fetch only messages from our process)
    public static void main(Args _args)
    {
	Counter  infoLogStartPos;
	try
	{ 
	    infoLogStartPos = infologLine();

	    // Your code
	}
	catch
	{
	    Counter infoLogEndPos = infologLine();
	    str logText           = Info::infoCon2Str(infolog.copy(infoLogStartPos, infoLogEndPos));

	    warning(logText);// You can save this log text into your table.
		
	    infoLogStartPos       = infoLogEndPos + 1;
	}
    }


  • Way 4  (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 5
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 6 (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 7
    infolog.text(infologLine());   
  • Way 8
 	str errormessage;
	errormessage = infolog.text(infologLine());
	errormessage = infolog.text(infologLine()-1);
	errormessage = errormessage + " "+ infolog.text(infologLine());
	Error(errormessage);
 
-----------------------------------Catch API Exception-----------------------------
catch (ex)
{
    isSuccess = true;

    if (ex is System.Net.WebException)
    {
        webResponse2 = (ex as System.Net.WebException).Response;

	using (System.IO.StreamReader responseStream = new System.IO.StreamReader(webResponse2.GetResponseStream()))
	{
	    str responseJson = responseStream.ReadToEnd().ToString();

	    if (responseJson)
	    {
		Map map = RetailCommonWebAPI::getMapFromJsonString(responseJson);

		if (map && map.exists('Message'))
		{
		    throw Global::error(map.lookup('Message'));
		}
		else
		{
		    ex = CLRInterop::getLastException().GetBaseException();

		    throw Global::error(ex.get_Message());
		}
	    }
	    else
	    {
		ex = CLRInterop::getLastException().GetBaseException();

		throw Global::error(ex.get_Message());
	    }
        }
    }
    else
    {
	ex = CLRInterop::getLastException().GetBaseException();

	throw Global::error(ex.get_Message());
    }

}
finally
{
    if (isSuccess == false)
    {
	if (ex is System.Net.WebException)
	{
	    webResponse2 = (ex as System.Net.WebException).Response;

	    using (System.IO.StreamReader responseStream = new System.IO.StreamReader(webResponse2.GetResponseStream()))
	    {
	        str  responseJson = responseStream.ReadToEnd().ToString();
		if (responseJson)
		{
		    Map map = RetailCommonWebAPI::getMapFromJsonString(responseJson);

		    if (map && map.exists('Message'))
		    {
			Global::error(map.lookup('Message'));
		    }
		    else
		    {
			ex = CLRInterop::getLastException().GetBaseException();
			Global::error(ex.get_Message());
		    }
		}
		else
		{
		    ex = CLRInterop::getLastException().GetBaseException();
		    Global::error(ex.get_Message());
		}
	    }
        }
        else
        {
	    ex = CLRInterop::getLastException().GetBaseException();
	    Global::error(ex.get_Message());
	}
    }
}   

Keep daxing!!

No comments:

Post a Comment