Monday, 18 January 2016

Writing in the event log from Dynamics AX

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.






static void EventViewer(Args _args)
{
    System.Diagnostics.EventLog eventlog;
    #Define.LogSource("Dynamics AX")
    #Define.LogName("Dynamics AX Log")
   
    ;
    // check if the log already exists
    if(!System.Diagnostics.EventLog::SourceExists(#LogSource))
    {
        // create new log
        System.Diagnostics.EventLog::CreateEventSource(#LogSource, #LogName);
    }

    eventlog = new System.Diagnostics.EventLog();
    eventlog.set_Source(#LogSource);

    // write info entry
    eventlog.WriteEntry("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);
   
    info("Check the event viewer!");

}

No comments:

Post a Comment