Write into the System Event Viewer from Dynamics AX

This is a very short article which shows how to write any information into the system event viewer. This is something that can be very useful when trying to debug processes such as batches that are difficult to debug. One thing to note is that if the code is running on the AOS the information will be written to the EventViewer on the AOS, just something to keep in mind if you don’t see your message in the EventViewer.

   1:  static void writeEventLogEntry(Args _args)
   2:  {
   3:      System.Diagnostics.EventLog eventlog;
   4:      #Define.LogSource("Dynamics AX")
   5:      #Define.LogName("Application") 
   6:      ;
   7:      new InteropPermission(InteropKind::ClrInterop).assert();  
   8:      // check if the log already exists
   9:      if(!System.Diagnostics.EventLog::SourceExists(#LogSource))
  10:      {
  11:          // create new log
  12:          System.Diagnostics.EventLog::CreateEventSource(#LogSource, #LogName);
  13:      }
  14:   
  15:      eventlog = new System.Diagnostics.EventLog();
  16:      eventlog.set_Source(#LogSource);
  17:   
  18:      // write info entry
  19:      eventlog.WriteEntry("<Info>: Just writing in the event viewer.");
  20:   
  21:      // write error entry
  22:      eventlog.WriteEntry("<Error>: Please check the stack trace below. nn" + 
  23:  con2str(xSession::xppCallStack()), System.Diagnostics.EventLogEntryType::Error);
  24:   
  25:      // write warning entry
  26:      eventlog.WriteEntry("Job finished." , System.Diagnostics.EventLogEntryType::Warning);
  27:      CodeAccessPermission::revertAssert();
  28:  }

Hope this possibly helps someone down the road somewhere!.

Join me on facebook and feel free to post your comments / feedback / queries.

See also:
Get MS Dynamics Ax Call Stack in X++ Code

If you liked the story and want to see more of this kind of content, please follow our twitter handle @CursorRun or FB page @CursorRun
Default image
Rahul Sharma
Rahul is a technology enthusiast, solutions architect, trainer, and blogger, working on various Microsoft and open source solutions with more than 18 years of industry experience. He specially takes interest in designing enterprise applications, cloud integrations, IoT, and other architecture rich business solutions.

Leave a Reply

%d bloggers like this: