Write to Windows event log: Difference between revisions

From Rosetta Code
Content added Content deleted
({{omit from|ZX Spectrum Basic}})
Line 2: Line 2:
Write script status to the Windows Event Log
Write script status to the Windows Event Log


=={{header|C sharp|C#}}==
=={{header|C sharp}}==
In Windows Vista and later or Windows Server 2003, you must have administrative privileges to execute this code.
In Windows Vista and later or Windows Server 2003, you must have administrative privileges to execute this code.
<lang csharp>using System.Diagnostics;
<lang csharp>using System.Diagnostics;
Line 24: Line 24:
}
}
}</lang>
}</lang>

=={{header|Delphi}}==
=={{header|Delphi}}==
<lang Delphi>program WriteToEventLog;
<lang Delphi>program WriteToEventLog;

Revision as of 20:20, 30 June 2011

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

C#

In Windows Vista and later or Windows Server 2003, you must have administrative privileges to execute this code. <lang csharp>using System.Diagnostics;

namespace RC {

 internal class Program
 {
   public static void Main()
   {
     string sSource  = "Sample App";
     string sLog     = "Application";
     string sEvent   = "Hello from RC!";
     if (!EventLog.SourceExists(sSource))
       EventLog.CreateEventSource(sSource, sLog);
     EventLog.WriteEntry(sSource, sEvent);
     EventLog.WriteEntry(sSource, sEvent, EventLogEntryType.Information);
   }
 }

}</lang>

Delphi

<lang Delphi>program WriteToEventLog;

{$APPTYPE CONSOLE}

uses Windows;

procedure WriteLog(aMsg: string); var

 lHandle: THandle;
 lMessagePtr: Pointer;

begin

 lMessagePtr := PChar(aMsg);
 lHandle := RegisterEventSource(nil, 'Logger');
 if lHandle > 0 then
 begin
   try
     ReportEvent(lHandle, 4 {Information}, 0, 0, nil, 1, 0, @lMessagePtr, nil);
   finally
     DeregisterEventSource(lHandle);
   end;
 end;

end;

begin

 WriteLog('Message to log.');

end.</lang>

PureBasic

<lang PureBasic>Procedure WriteToLog(Event_App$,EventMessage$,EvenetType,Computer$)

 Protected wNumStrings.w, lpString=@EventMessage$, lReturnX, CMessageTyp, lparray
 Protected lprawdata=@EventMessage$, rawdata=Len(EventMessage$), Result
 Protected lLogAPIRetVal.l = RegisterEventSource_(Computer$, Event_App$)
 If lLogAPIRetVal
   lReturnX = ReportEvent_(lLogAPIRetVal,EvenetType,0,CMessageTyp,0,wNumStrings,rawdata,lparray,lprawdata
   DeregisterEventSource_(lLogAPIRetVal)
   Result=#True
 EndIf
 ProcedureReturn Result

EndProcedure</lang>

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>