Usually it will be difficult to monitor/debug Batch Jobs running on server . For example if something goes erroneous than it's not easy to find out from where and how its arising.
So we can make use of Event viewer instead of infolog and monitor the status by checking the event viewer .
Writing to the event log in Windows using AX is very easy when you use the EventLog class from the System.Diagnostics namespace. The following job demonstrates how to use the EventLog class.
staticvoid writeEventLogEntry(Args _args)
{
System.Diagnostics.EventLog eventlog;
#Define.LogSource("Dynamics AX")
#Define.LogName("Application")
;
// check if the log already exists
if(!System.Diagnostics.EventLog::SourceExists(#LogSource))
{
// create new log
System.Diagnostics.EventLog::CreateEventSource(#LogSource, #LogName);
}
eventlog = newSystem.Diagnostics.EventLog();
eventlog.set_Source(#LogSource);
// write info entry
eventlog.WriteEntry("<Info>: Just writing in the event viewer.");
// write error entry
eventlog.WriteEntry("<Error>: Please check the stack trace below. \n\n" +
con2str(xSession::xppCallStack()), System.Diagnostics.EventLogEntryType::Error);
// write warning entry
eventlog.WriteEntry("Job finished." , System.Diagnostics.EventLogEntryType::Warning);
}
Also there is another way by which you can trace the info or error in to event viewer.
The following code shows you how to write event log entry with X++:
Create a new class AX_EventLog with a static method WriteEventLog:
static void WriteEventLog(Exception _exception, str _event)
{
str eventSource = "AX event";
str logType = "Application";
System.Diagnostics.EventLogEntryType eventLogEntryType;
int eventCategory = 9999;
;
{
str eventSource = "AX event";
str logType = "Application";
System.Diagnostics.EventLogEntryType eventLogEntryType;
int eventCategory = 9999;
;
switch(_exception)
{
case Exception::Info:
eventLogEntryType = System.Diagnostics.EventLogEntryType::Information;
break;
case Exception::Warning:
eventLogEntryType = System.Diagnostics.EventLogEntryType::Warning;
break;
{
case Exception::Info:
eventLogEntryType = System.Diagnostics.EventLogEntryType::Information;
break;
case Exception::Warning:
eventLogEntryType = System.Diagnostics.EventLogEntryType::Warning;
break;
default:
eventLogEntryType = System.Diagnostics.EventLogEntryType::Error;
}
eventLogEntryType = System.Diagnostics.EventLogEntryType::Error;
}
if (!System.Diagnostics.EventLog::Exists(eventSource))
{
System.Diagnostics.EventLog::CreateEventSource(eventSource, logType);
}
{
System.Diagnostics.EventLog::CreateEventSource(eventSource, logType);
}
System.Diagnostics.EventLog::WriteEntry(eventSource, _event, eventLogEntryType, eventCategory);
}
}