Rahul Sharma

Solutions Architect - Microsoft Dynamics AX | Azure

This is a technology blog covering Microsoft Dynamics 365, Microsoft Azure, IoT, Power Apps, Power BI, .Net, SQL Server, SSRS, and related technologies. Join this blog on facebook {Rahul Sharma}, to start a discussion !!! NOTE: My employer is not responsible for the content published here.

Index | MS Dynamics AX | MS Dynamics CRM
View blog as >> Magazine | Sidebar | Flipcard | Mosaic | Snapshot | Timeslide

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:      }
  15:      eventlog = new System.Diagnostics.EventLog();
  16:      eventlog.set_Source(#LogSource);
  18:      // write info entry
  19:      eventlog.WriteEntry("<Info>: Just writing in the event viewer.");
  21:      // write error entry
  22:      eventlog.WriteEntry("<Error>: Please check the stack trace below. \n\n" + 
  23:  con2str(xSession::xppCallStack()), System.Diagnostics.EventLogEntryType::Error);
  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