Propagate Infolog Messages to the Windows Event Log for AX 2012

This topic describes how you can propagate Microsoft Dynamics AX Infolog messages to the Windows Event Log.


there are 2 ways :
1. through Terminal client configuration file change
2. log the event through code


1. through Terminal client configuration file change:

To propagate Infolog messages, edit the AX32.exe.config XML file to add listeners from the .NET System.Diagnostics namespace. One listener that you can add is the EventLogTraceListener. After you add this listener, all messages that are written to the Infolog are also written to the Windows Event Log. Your .NET program can search the Windows Event Log for messages that have the source name of Dynamics Infolog.

XML for infolog listener:

You can copy the whole <system.diagnostics> section of following XML code into your AX32.exe.config file. The AX32.exe program reads the config file each time the program is restarted.

<configuration>

<system.diagnostics>
<trace autoflush="true"/>
<sources>
<source name="Microsoft.Dynamics.Kernel.Client.DiagnosticLog-Infolog"
switchValue="Information">
<listeners>
<add name="EventLog"
type="System.Diagnostics.EventLogTraceListener"
initializeData="Dynamics Infolog"/>
<add name="TextFile"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="DynamicsInfologTrace.log"
traceOutputOptions="DateTime"/>
</listeners>
</source>
</sources>
</system.diagnostics>

</configuration>

Note:

The AX32.exe.config file is located in the same client bin directory where AX32.exe is located. The whole path might be as follows:

C:\Program Files (x86)\Microsoft Dynamics AX\<versionNumber>\Client\Bin\


 Open the file in notepad and add some additional XML sections in order to trigger the functionality. You can copy from the MSDN article or just type in what I have used.

 now start the client .. all the infologs to created to the event viewer .
In order to test this, let us create a simple job that triggers a Info, Warning and Error message.



Check the event viewer with source name of Dynamics Infolog.  you will see the events logged.

-------------------------------------------
2. log the event through code

Use the static method below: call this static method from your code .. pass the message to be logged and exception type. you will see events logged under event viewer

static void WritetoWindowsEventLog(str _eventMessage,Exception _exceptionType = Exception::Info)
{
System.Diagnostics.EventLog eventLog;
str eventSource = 'Dynamics AX custom event';
System.Diagnostics.EventLogEntryType eventLogEntryType;

switch(_exceptionType)
{
case Exception::Error:
eventLogEntryType = System.Diagnostics.EventLogEntryType::Error;
break;

case Exception::Warning:
eventLogEntryType = System.Diagnostics.EventLogEntryType::Warning;
break;
default:
eventLogEntryType = System.Diagnostics.EventLogEntryType::Information;
}
eventLog = New System.Diagnostics.EventLog();
eventLog.set_Source(eventSource);
eventLog.WriteEntry(_eventMessage, eventLogEntryType);
//System.Diagnostics.EventLog::WriteEntry(eventSource, _eventMessage, eventLogEntryType);
}

__

http://www.artofcreation.be/2009/06/19/writing-in-the-event-log-from-dynamics-ax/

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!");
}

I have created a custom view to show only - Dynamics AX custom event



__

Comments