Quick Modification for Tracing Bug Code in a Dynamics AX Production Environment
This quick modification for tracing bug code in a Dynamics AX production environment was used at a client site. I have received their permission to blog about this as I thought it was pretty useful.
Basically, the modification consists of the creation of an empty security role. When a user reports a bug, they may be asked for more detailed repro steps within the production environment. The user will be added to this security role, which will trigger the dumping of the X++ call stack into the Infolog when the error is generated. Once the repro is complete, the user is simply removed from the security role, and the logging is stopped. For the bug repro, the user may need to have running code in CIL disabled so the full call stack can be retrieved as well.
Part 1. Create an empty security role in the system
- Navigate in the Dynamics AX 2012 client System administration > Setup > Security > Security roles
- Click the New button
- Fill in pertinent information
- Click the Close button
Part 2. Modify the info class, add() method
Change the method to the following code (no changes in the latter half of the method, so just make the displayed modifications:
Exception add( Exception _exception, str _txt, str _helpUrl = '', Object _sysInfoAction = null, boolean buildprefix = true) { int numOfLines,i; int actionClassId; container packedAction; xSession session; // START - Add callstack to infolog #Define.mySecurityRole('EnableAXDebugging') System.Diagnostics.StackTrace myStackTrace; container myCallStack; int cntr; str1260 whatToWrite; SecurityUserRole securityUserRole; SecurityRole securityRole; boolean dumpCallStack = false; while select AotName from securityRole join securityUserRole where securityUserRole.User == curUserId() && securityUserRole.SecurityRole == securityRole.RecId { if (securityRole.AotName == #mySecurityRole) { // if we find the security role, set our X++ call stack dump variable, and quit searching as we are done dumpCallStack = true; break; } } if (dumpCallStack) { // determine if running as native X++ or as IL code if (xSession::isCLRSession()) // IL code { myStackTrace = new System.Diagnostics.StackTrace(true); _txt += ' - ' + myStackTrace.ToString(); } else { myCallStack = xSession::xppCallStack(); for(cntr=1; cntr<=conlen(myCallStack); cntr++) { whatToWrite += conpeek(myCallStack, cntr); if (cntr mod 2) { whatToWrite += '-Line: '; } else { whatToWrite += "\r\n"; } } _txt += ' \r\n Call stack: ' + whatToWrite; } } // END - Add callstack to infolog
NOTE: there is a rough stub for capturing the CIL call stack as well.
Part 3. Add the security role to the user
Within the client, navigate System Administration > Common > Users. Select the user by double-clicking, and add the security role to the user by clicking the Assign roles button
Click the OK button once the debugging role has been selected.
Part 4. If needed, disable running business logic in CIL
In the client, navigate File > Tools > Options > Development tab. Uncheck the Execute business operations in CIL checkbox
Click the Close button.
At this point, you should be able to run a process, and see the call stack in the Infolog when the error is generated.
Once this has been captured, simply go back into the user, and remove the debugging security role from the user. If the Execute business operations in CIL checkbox was modified, go back and correct that setting as well.
I hope that you find this useful, and it provides you something you can add to your ‘bag of tricks’ for debugging or troubleshooting any Dynamics AX issues.
Under the terms of this license, you are authorized to share and redistribute the content across various mediums, subject to adherence to the specified conditions: you must provide proper attribution to Stoneridge as the original creator in a manner that does not imply their endorsement of your use, the material is to be utilized solely for non-commercial purposes, and alterations, modifications, or derivative works based on the original material are strictly prohibited.
Responsibility rests with the licensee to ensure that their use of the material does not violate any other rights.