I haven’t written a useful blog post about Dynamics AX in a while, so here’s something I came across this week that might be interesting. I was trying to troubleshoot a problem with a fairly complicated process that I’ve somehow wound up being in charge of. In digging through the code, I found several instance of this pattern:
// do some stuff involving database reads and writes
…and that’s it. If something goes wrong, abort everything and keep quiet about it! (I guess I should call that an anti-pattern rather than a pattern.) I think this will still display any error messages to the end-user, but of course there’s no guarantee that the end-user will tell anyone.
Well, I highly suspect that things may be going wrong somewhere in one of these try blocks, given what I’m seeing in the database, so I wanted to add some logging, at least, to the catch blocks.
In AX, error messages generally get thrown into the infolog, which is a nice little mechanism for queuing up messages for the end-user to see. So, what I’d want to do in the catch block is grab any messages from the infolog and e-mail them to myself. I looked around for other code that was doing this, and found a few instances of something like this:
s = infolog.text();
…then ‘s’ would be emailed to someone, or saved off to a log file. This looked good, but I wanted to check it first, so I wrote a little test job to try it. Well, infolog.text() really only returns the first line from the log, so that’s probably not what I want. (I’m not sure if the programmer who wrote the code I was looking at only wanted the first line, or if he just didn’t realize that infolog.text() only returns one line.) So I dug a little deeper, and found that:
c = infolog.infologData();
will get the entire contents of the infolog into container ‘c’. But it also clears the infolog, so the user doesn’t see the error messages, so that’s not good.
Digging some more, I found that:
c = infolog.copy(1, infologLine());
will copy the entire infolog to a container, without clearing it, so the user can still see it. So then I tried the usual con2str() method to convert the container to a string I could email to myself. But, it turns out that it’s a structured container that can’t be converted to string that easily. So then I found info::infoCon2Str(), which parses out the infolog container structure to string, with the parts delimited by pound signs. So to break that back up into lines I can replace # with \n and off I go.
I managed to get that all into a one-liner that looks like this:
s = strReplace(info::infoCon2Str(infolog.copy(1, infologLine())), '#', '\n');
Not bad, right? Not perfect either, and you can get a more nicely formatted string out of it by writing a little utility method to parse out the container into a friendlier format. (See this blog post for an example. Look at Martin Drab’s comment below the post for a useful code snippet.)