How to Send the Callstack to the Infolog

When I worked for Microsoft I wrote a blog on how to send the X++ callstack in AX to the infolog.  Here is that post: http://blogs.msdn.com/b/axsupport/archive/2010/08/02/how-to-send-the-callstack-to-the-infolog.aspx

Sending the callstack to the infolog is extremely helpful when troubleshooting an issue that happens sporadically or when you are in a production environment and cannot run the debugger.

However, in AX 2012 a lot of code is executed as IL code like posting sales orders, purchase orders or running any process in batch. When code is executed as IL code the steps listed in the blog above do not work and will throw exceptions. We therefore need a different approach for capturing the IL and/or X++ callstack. The code below shows you how to grab the callstack regardless of how the code is executed. Enjoy and happy programming!

static void THK_infoMyStackTrace(Args _args)
{
    System.Diagnostics.StackTrace myStackTrace = new System.Diagnostics.StackTrace(true); System.Exception ex;

    if (xSession::isCLRSession()) 
    { 
        try 
        { 
            info(myStackTrace.ToString()); 
        } 
        catch 
        { 
            ex = ClrInterop::getLastException(); 
            if (ex != null) 
            { 
                ex = ex.get_InnerException(); 
                if (ex != null) 
                { 
                    info(ex.ToString()); 
                } 
            } 
        } 
    } 
    else 
    { 
        info(con2Str(xSession::xppCallStack())); 
    }
}
原文地址:https://www.cnblogs.com/Fandyx/p/2943897.html