Ax 2012 and ETW (Event Tracing for Windows)

Now let’s get into to business quick and start off with a question : “How often did you want to reproduce / debug a certain process without actually knowing where the source of the problem may reside?”

Well the answer to that question for most of us will be : “Quite a lot” and then this would be followed immediately with the question : “Did you then create data log tables to store values, stack traces, information messages, …?” And again the answer will be often : “Yes”

Now this will probably help you out : ETW!

I will not go into the full details of Event Tracing for Windows as this would definately take us too far, but here’s a link where you can do some additional reading : http://msdn.microsoft.com/en-us/library/windows/desktop/bb968803(v=vs.85).aspx

What we will look at for this post is how we can make advantage of ETW to do some tracing within Dynamics Ax 2012. Specifically we are going to make a custom piece of code do output some informational messages that can be caught by a data collector set using Perfmon and viewed by the Event viewer.

The following question may arise : “Why would we want ETW to handle the tracing and not stay in our habitat of X++ and storing data in tables?” Well the answer to this is:

  • ETW by far does not cause as many overhead as normal logging would do. Both for execution time (CPU) and diskspace.
  • If your code is ETW ready, you can do logging in production environments by starting / stopping data collector sets.
  • If your code is ETW ready, logging does not need to be built when needed but will immediately be available when needed.
  • The log files can be used in Event Viewers and also SCOM so it helps system administrators to know what is going on in the black box Dynamics Ax.

So let us jump in and create the data collector set.

First start op by opening perfmon and navigate to the user defined data collector sets.

Right click and create a new data collector set.

 

 

After the creation of the data collector set, right click it and you can start / stop the collection of ETW events.

But now that we have the collection part, we also need to make custom code trigger some events from within Dynamics Ax. And that is where the xClassTrace  class comes into play. The xClassTrace class is used to make advantage of ETW and is used to start / stop traces to files, log component messages in ETW to be caught by data collector sets, …

So let us take a look at some sample code that loops all customers and logs a component message in ETW to be caught. (Please not the isTracingEnabled checks to make sure string formatting will only be done when needed to have the minimum amount of performance overhead)

static void KeSae_ETW_LogComponentMessageTest(Args _args)
{
    CustTable   theCustomer;
    ;

    if(xClassTrace::isTracingEnabled())
    {
        xClassTrace::logComponentMessage('AxTracing',"Starting ETW tracing");
    }

    while select theCustomer
    {
        if(xClassTrace::isTracingEnabled())
        {
            xClassTrace::logComponentMessage('AxTracing',strFmt("Processing customer %1", theCustomer.AccountNum));
        }
    }

    if(xClassTrace::isTracingEnabled())
    {
        xClassTrace::logComponentMessage('AxTracing',"Stopping ETW tracing");
    }
}

Now that our code is ready to log some messages, start the data collector set in the perfmon tool and run the sample code. After running the code, go ahead and stop the data collector set. The result will be a log file generated that can be openened by the Event Viewer as seen below where we can actually see the logged messages from within Dynamics Ax.

So this is a very simple example, but this logging method is the way to go when you want efficient, performing logging in your system! This way you can minimize the logging overhead.

Leave a Reply

Your email address will not be published. Required fields are marked *