Enable Windows event Log In Axapta


I have implemented Dynamics AX Event Logs in Test environment, This can be particularly useful when you would like to use the Windows Event Log to monitor AX batch Jobs.

For getting this feature enabled,I have added a new static method called writeLogEntry to the Global class:

static void writeLogEntry(Exception e, str caller, int line, str text)
{
// Use the standard .NET class EventLog from
// the System.Diagnostics assembly
System.Diagnostics.EventLog eventLog;
// Also use a .NET enumeration from the
// System.Diagnostics assembly
System.Diagnostics.EventLogEntryType entryType;
System.Exception clrException;
str stack;
Batch batch;
str batchInfo;
;
try
{
// Create a new object of the EventLog class
eventLog = new System.Diagnostics.EventLog();
eventLog.set_Source("Dynamics AX");
// Set the enumeration value based on the Exception
// type in AX
switch (e)
{
case Exception::Info :
entryType =
System.Diagnostics.EventLogEntryType::Information;
break;
case Exception::Warning :
entryType =
System.Diagnostics.EventLogEntryType::Warning;
break;
case Exception::Error :
entryType =
System.Diagnostics.EventLogEntryType::Error;
break;
}
// If the current user is running a batch job
// we can assume that the info message came
// from the batch job and add additional information
// to the event log

while select batch
where batch.Status == BatchStatus::Executing &&
batch.ExecutedBy == curuserid()
{
batchInfo += batch.GroupId + ': '+
classid2name(batch.ClassNumber) + '\n';
}
if (batchInfo)
eventLog.WriteEntry(strfmt("Batch info from AX: %1 \n\n
The message originated from :%2 \nat line %3 \n\n
Message: %4", batchInfo, caller, line, text),
entryType);
else
eventLog.WriteEntry(strfmt("Info from AX: \n\n
The message originated from :%1 \nat line %2 \n\n
Message: %3", caller, line, text), entryType);
}
catch(Exception::CLRError)
{
// If not able to write the info to the eventlog
// print an error in the print window instead.
print "EventWriter:
Unable to write entry to the windows eventlog";
}
}

Then I have added a line of code at the end of the add method in the Info class so that the
end of the method will look like this:

writeLogEntry(_exception, conpeek(packedAction,2) ,
conpeek(packedAction,3), _txt);
this.addSysInfoAction(_helpUrl, actionClassId, packedAction);
}
return super(_exception, (buildprefix?getprefix():'')+_txt);
}

To test the feature, simply create a new Job that prints something to the infolog:

static void TestEventLog(Args _args)
{
;
info("This is a test");
}

We will now see an infolog message in AX, and if you open the Windows EventViewer we should see the following message in the list:


Double-clicking on the event will bring up information about the origin of the info
and the message that was printed to the infolog in AX.


This example could easily be extended so you can enable or disable the feature fordifferent users and select the level of messages to be sent to the EventLog.

Comments