Retrieving Eventlog messages in Navision ?

ehsehs Member Posts: 34
edited 2008-06-05 in NAV Tips & Tricks
Has anyone found an easy way to retrieve messages from the Windows Eventlog (Application log) in to Navision ?

I was thinking of using an existing Windows automation or a freeware/shareware component but I can't find out which one to use.

What I want is to make a codeunit able to import all Windows application log entries in to a Navision table (or set a filter so that only application log entries related to the active NAS is being imported).

The Eventlog controls I have found are quite expensive - is it not possible to do this with native Microsoft components or has any one in here created an automation/ocx for this purpose ?


  • DenSterDenSter Member Posts: 8,304
    I have not done this particular thing, but have programmed many other types of automation just like this, and I have programmed event log operations in other languages.

    Try to find a good code sample for event log operations (I used to have a brilliant article about this but my link doesn't work anymore) for .NET like VB or C#. You can usually find the dll in the automation list inside the C/AL editor. Then it's just a matter of getting it to work in NAV.
  • ehsehs Member Posts: 34
    Thanks DenSter,

    but that is exactly my problem: I can't find a suitable dll or ocx.

    The only ones I can find are fancy 200 - 400 $ components and the only thing I want to do is to read the eventlog.

    Does anyone else have a link or referance to component with a simple code example ?
  • Luc_VanDyckLuc_VanDyck Member, Moderator, Administrator Posts: 3,633
    Have a look at this topic: Read EventLog
    No support using PM or e-mail - Please use this forum. BC TechDays 2024: 13 & 14 June 2024, Antwerp (Belgium)
  • rvduurenrvduuren Member Posts: 92
    I've created some C# code, and it results a xml file of the Event Log..

    Here's the Dynamics Nav Code
    EventLog - Automation - 'XMLEventLog'.ReadEventLog
    EventLog.GetEventLog('Application', 'myhost', 'c:\xmlfile.xml'); // Application/System/...

    Download:: Source, Compiled And Setup

    Please post bugs here..

    cSharp Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.Data;
    using System.Xml;
    using System.Diagnostics;
    namespace XMLEventLog
        public interface IReadEventLog
            void GetEventLog(string LogEventType, string HostName, string XmlFileName);
        public class ReadEventLog : IReadEventLog
            public void GetEventLog(string LogEventType, string HostName, string XmlFileName)
                DataTable dataTable = new DataTable("Log");
                dataTable.Columns.Add("Index", Type.GetType("System.Int32"));
                dataTable.Columns.Add("EntryType", Type.GetType("System.String"));
                dataTable.Columns.Add("TimeGenerated", Type.GetType("System.DateTime"));
                dataTable.Columns.Add("Source", Type.GetType("System.String"));
                dataTable.Columns.Add("UserName", Type.GetType("System.String"));
                dataTable.Columns.Add("MachineName", Type.GetType("System.String"));
                dataTable.Columns.Add("Message", Type.GetType("System.String"));
                DataRow dataRow;
                EventLog eventLog = new EventLog(LogEventType, HostName);
                if (eventLog.Entries.Count > 0)
                    foreach (EventLogEntry eventLogEntry in eventLog.Entries)
                        dataRow = dataTable.NewRow();
                        dataRow["Index"] = eventLogEntry.Index;
                        dataRow["EntryType"] = eventLogEntry.EntryType;
                        dataRow["TimeGenerated"] = eventLogEntry.TimeGenerated;
                        dataRow["Source"] = eventLogEntry.Source;
                        dataRow["UserName"] = eventLogEntry.UserName;
                        dataRow["MachineName"] = eventLogEntry.MachineName;
                        dataRow["Message"] = eventLogEntry.Message;
                    DataSet dataSet = new DataSet(LogEventType);
                    XmlDataDocument xmlDatDoc = new XmlDataDocument(dataSet);
    Met vriendelijke groet, best regards,

  • ehsehs Member Posts: 34
    Thank you Luc and rvduuren,

    I have used the link from Luc because this was based on standard MS components only (I did search before posting this topic but didn't find the link in the search result).

    With the solution from ara3n (the link from Luc) I can read the eventlog from a codeunit (a create(WindowsShell) was missing in the example but I figured that out).

    The example shows how to read the latest entry in the log but I want to read them all and insert them into a table.

    This should be possible by changing the /r parameter but if I increase this to a value larger than 7 I get an error message in Navision saying "The length of the text string exceeds the size of the string buffer."

    This works (reading 1 eventlog entry):

    IF EventRec.FIND('+') THEN
    NextEvent := EventRec."Event No." ELSE
    NextEvent := EventRec."Event No.";
    NextEvent := NextEvent + 1;
    WindowShell.CurrentDirectory(ENVIRON('windir') + '\system32\');
    txtCommand := 'cscript eventquery.vbs /L Application /FI "Type eq Error" /V /r 1 /FO "LIST"';
    WSHExec := WindowShell.Exec(txtCommand);
    WSHTextStream := WSHExec.StdOut;
    NextEventLine := 0;
    NextEventLine := NextEventLine + 1;
    EventRec."Event No." := NextEvent;
    EventRec."Event Line No." := NextEventLine;
    EventRec.Text := COPYSTR(WSHTextStream.ReadLine,1,250);
    UNTIL WSHTextStream.AtEndOfStream;

    This doesn't work (reading more than 7 entries):

    txtCommand := 'cscript eventquery.vbs /L Application /FI "Type eq Error" /V /r 8 /FO "LIST"';

    Can anybody help or suggest a workaround for this ?
  • kinekine Member Posts: 12,562
    try to read them one by one using the N1-N2 syntax for parameter /r. See ... x?mfr=true for more help.
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • ehsehs Member Posts: 34
    I have already tried that to with the same result/error message.

    If I could only delete the message after having read it that would do the trick, but the script is for reading only :(
  • kinekine Member Posts: 12,562
    I have one info for you: the script doesn't exists under Vista... 8) (but you can use the "wevtutil" to query the event log under Vista).

    1) At which line stop the debugger when you try to read more than 8 events?
    2) Try to compare the output of the script when called from command line. It seems that some line from the output is longer than your destination.
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • ehsehs Member Posts: 34
    It stops with the error at the following line :

    EventRec.Text := COPYSTR(WSHTextStream.ReadLine,1,250);

    ..which is exactly why I'm using the COPYSTR command (and yes I have checked that the Text field is defined as text with the length 250).

    I seems that it is WSHTextStream.ReadLine which can't read long strings.
  • kinekine Member Posts: 12,562
    Than you need to use Read(Chars) instead ReadLine to limit how much chars you want to read. You can read the text char by char and process them into string with max length 250.
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • krikikriki Member, Moderator Posts: 9,079
    [Topic moved from Navision forum to Navision Tips & Tricks forum]
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!

Sign In or Register to comment.