Bojensen Blogs

How to read from the Windows event log from Dynamics Ax

The Windows event log is a valuable source of information, for both hardware and software events.

And the good news is, you can read and write from within Dynamics Ax.  So if you want, you can even write your own events in the Application event log.  Or maybe you want to build some monitoring tool with Dynamics Ax, so you need to read the Security event log from a certain computer.

Here is an example of reading in the Application event log.

static void myEventLogReader(Args _args)
    System.Diagnostics.EventLog                     myEventLog;
    System.Diagnostics.EventLogEntryCollection      myEventLogEntryCollection;
    System.Diagnostics.EventLogEntry                myEventLogEntry;
    System.Diagnostics.EventLogEntryType            myEventLogEntryType=ClrInterop::parseClrEnum('System.Diagnostics.EventLogEntryType','Error');
    str                                             logName='Application';
    str                                             machineName=".";
    int                                             logentries;
    int                                             counter;
    System.DateTime                                 genDateTime;
    TransDateTime                                   mygenDateTime;
    str                                             logmessage;
    myEventLog = new System.Diagnostics.EventLog(logName,machineName);
        myEventLogEntry = myEventLogEntryCollection.get_Item(counter); 
            error(strfmt('%1 %2',dateTimeUtil::toStr(mygenDateTime)+' '+logmessage));

In the example above we use EventLog, EventLogEntry and EventLogEntryCollection, all from the System.Diagnostics namespace.

Only events of type Error are shown.  To perform the check, we use some form of enumeration.  I’ve blogged about this in the past, read more about it here.

The example above can be customized.  You can specify another computer, by changing the machineName variable.  Default here is “.”, which is the local computer.  (If no machine name is specified, the local computer is assumed.)

Also possible to change is which log is read.  This defaults to the Application log, but you can change this by specifying your own logname.

Comments are closed.