Write to Windows event log: Difference between revisions

From Rosetta Code
Content added Content deleted
(Creating new task page for Writing to Windows Event Log; only covers PowerShell language, for now)
 
(→‎Tcl: Added implementation)
Line 3: Line 3:


=={{header|PowerShell}}==
=={{header|PowerShell}}==
<lang powershell>
<lang powershell># Create Event Log object

# Create Event Log object
$EventLog=new-object System.Diagnostics.EventLog("Application")
$EventLog=new-object System.Diagnostics.EventLog("Application")
#Declare Event Source; must be 'registered' with Windows
#Declare Event Source; must be 'registered' with Windows
Line 17: Line 15:


# Write the event in the format "Event test",EventType,EventID
# Write the event in the format "Event test",EventType,EventID
$EventLog.WriteEntry("My Test Event",$infoevent,70)
$EventLog.WriteEntry("My Test Event",$infoevent,70)</lang>

</lang>
''Note1:'' Thanks to PoSH Fan for posting information that got me started on this at [http://winpowershell.blogspot.com/2006/07/writing-windows-events-using.html Windows PowerShell Blog]
''Note1:'' Thanks to PoSH Fan for posting information that got me started on this at [http://winpowershell.blogspot.com/2006/07/writing-windows-events-using.html Windows PowerShell Blog]
<br />
<br>
''Note2:'' See details on registering a new Event Source with Windows at [http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.registerdisplayname.aspx MSDN]
''Note2:'' See details on registering a new Event Source with Windows at [http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.registerdisplayname.aspx MSDN]

=={{header|Tcl}}==
{{libheader|TWAPI}}
<lang tcl>package require twapi

# This command handles everything; use “-type error” to write an error message
twapi::eventlog_log "My Test Event" -type info</lang>

Revision as of 16:30, 8 May 2010

Task
Write to Windows event log
You are encouraged to solve this task according to the task description, using any language you may know.

Write script status to the Windows Event Log

PowerShell

<lang powershell># Create Event Log object $EventLog=new-object System.Diagnostics.EventLog("Application")

  1. Declare Event Source; must be 'registered' with Windows

$EventLog.Source="Application" # It is possible to register a new source (see Note2)

  1. Setup the Event Types; you don't have to use them all, but I'm including all the possibilities for reference

$infoEvent=[System.Diagnostics.EventLogEntryType]::Information $errorEvent=[System.Diagnostics.EventLogEntryType]::Error $warningEvent=[System.Diagnostics.EventLogEntryType]::Warning $successAuditEvent=[System.Diagnostics.EventLogEntryType]::SuccessAudit $failureAuditEvent=[System.Diagnostics.EventLogEntryType]::FailureAudit

  1. Write the event in the format "Event test",EventType,EventID

$EventLog.WriteEntry("My Test Event",$infoevent,70)</lang> Note1: Thanks to PoSH Fan for posting information that got me started on this at Windows PowerShell Blog
Note2: See details on registering a new Event Source with Windows at MSDN

Tcl

Library: TWAPI

<lang tcl>package require twapi

  1. This command handles everything; use “-type error” to write an error message

twapi::eventlog_log "My Test Event" -type info</lang>