EventLog Class
Contents
Overview
This .NET class offers a number of different opportunities to read and write event logs. There are three examples below covering the most frequent cases. Note that there is no example for reading from / writing to a remote event log.
Although it should be not a big problem to get this to work if you have proper rights to do so, there might be an even easier way: As an APL programmer your likely to be interested in writing to the "Application" class of the Windows Event Log. In that case the ready-for-usage-class WindowsEventLog is all you need, making the task very easy indeed.
The code was prepared with V11 of Dyalog.
Terms to be used
Classes
There are some pre-defined classes: "Application", "Internet Explorer", "Security", "System". You can define your own class which is also referred to as "Custom Event Class".
Application
Within a class or custom event log, you need to define an application, also referred to as "source".
Write to the "Application" Event Log
System∆Diagnostics∆EventLog∆01;Class;Name;myLog;i
⍝ Version 1.0 from 2007-01-01 ⋄ Kai Jaeger ⋄ APL Team Ltd
⍝ Write into the "Application" Event Log as application "ApplName"
(⎕ML ⎕IO)←1
⎕USING←'System.Diagnostics,System.dll' ''
Class←'Application' ⍝ maybe "System" or a custom name instead; "Security" is read-only
Name←'ApplName' ⍝ Typically the name of the application
⍝ Create an EventLog instance and assign its source. Since "Application" is a standard
⍝ event class (as "System" and "Security") we know it will already exist!
myLog←⎕NEW System.Diagnostics.EventLog(⊂Class)
myLog.Source←Name
⍝ Write an informational entry to the event log
myLog.WriteEntry'First writing to event log from Dyalog'System.Diagnostics.EventLogEntryType.Error
⍝ Instead of using "Name", we can also specify a free name:
myLog.WriteEntry'SpecialName' 'Second writing to event log from Dyalog'
⎕←(⍕myLog.Entries.Count),' entries in EventLog "',Name,'"'
:For i :In ⍳myLog.Entries.Count
⎕←(⍕i),'. ',{myLog.Entries.(get_Item ⍵).Message}i-1
:EndFor
myLog.Close
Read entries from the "System" Event Log
System∆Diagnostics∆EventLog∆02;Class;mySystemLog;i
⍝ Version 1.0 from 2007-01-01 ⋄ Kai Jaeger ⋄ APL Team Ltd
⍝ This time, we try to read the "System" log
(⎕ML ⎕IO)←1
⎕USING←'System.Diagnostics,System.dll' ''
Class←'System'
⍝ Create an EventLog instance
mySystemLog←⎕NEW System.Diagnostics.EventLog(⊂Class)
⎕←(⍕mySystemLog.Entries.Count),' entries in EventLog "',Class,'"'
:For i :In ⍳mySystemLog.Entries.Count
⎕←(⍕i),'. ',{mySystemLog.Entries.(get_Item ⍵).Message}i-1
:EndFor
mySystemLog.Close
Write to a custom Event Log
System∆Diagnostics∆EventLog∆03;Class;Name;myCustomLog;i
⍝ Version 1.0 from 2007-01-01 ⋄ Kai Jaeger ⋄ APL Team Ltd
⍝ Write into custom event class "MyCustomLog" as application "ApplName2"
(⎕ML ⎕IO)←1
⎕USING←'System.Diagnostics,System.dll' ''
⍝ Create the source, if it does not already exist (it will)
Class←'MyNewLog'
Name←'ApplName2'
⍝ This time we cannot be sure about the custom class, so let's check an create if needed:
:If ~System.Diagnostics.EventLog.SourceExists⊂Name
System.Diagnostics.EventLog.CreateEventSource Name Class
:EndIf
⍝ Create an EventLog instance and assign its source
myCustomLog←⎕NEW System.Diagnostics.EventLog(⊂Class)
myCustomLog.Source←Name
⍝ Write an informational entry to the event log
myCustomLog.WriteEntry'First writing to custom event log from Dyalog'System.Diagnostics.EventLogEntryType.Error
⍝ Instead of using "Name", we can also specify a free name:
myCustomLog.WriteEntry'SpecialName' 'Second writing to custom event log from Dyalog'
⎕←(⍕myCustomLog.Entries.Count),' entries in EventLog "',Name,'" of class "',Class,'"'
:For i :In ⍳myCustomLog.Entries.Count
⎕←(⍕i),'. ',{myCustomLog.Entries.(get_Item ⍵).Message}i-1
:EndFor
myCustomLog.Delete⊂Class
myCustomLog.CloseAuthor: KaiJaeger
APL Wiki