Write to Windows event log: Difference between revisions

m
(Added Wren)
m (→‎{{header|Wren}}: Minor tidy)
 
(4 intermediate revisions by 4 users not shown)
Line 4:
Write script status to the Windows Event Log
<br><br>
 
=={{header|11l}}==
{{trans|C}}
 
<syntaxhighlight lang="11l">:start:
I :argv.len != 5
print(‘Usage : #. < Followed by level, id, source string and description>’.format(:argv[0]))
E
os:(‘EventCreate /t #. /id #. /l APPLICATION /so #. /d "#."’.format(:argv[1], :argv[2], :argv[3], :argv[4]))</syntaxhighlight>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang="autohotkey">; By ABCza, http://www.autohotkey.com/board/topic/76170-function-send-windows-log-events/
h := RegisterForEvents("AutoHotkey")
SendWinLogEvent(h, "Test Message")
Line 195 ⟶ 204:
; Return the number of characters copied.
return char_count
}</langsyntaxhighlight>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f WRITE_TO_WINDOWS_EVENT_LOG.AWK
BEGIN {
Line 217 ⟶ 226:
}
function error(message) { printf("error: %s\n",message) ; errors++ }
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 225 ⟶ 234:
=={{header|Batch File}}==
The "EventCreate" command does the task.
<langsyntaxhighlight lang="dos">@echo off
EventCreate /t ERROR /id 123 /l SYSTEM /so "A Batch File" /d "This is found in system log."
EventCreate /t WARNING /id 456 /l APPLICATION /so BlaBla /d "This is found in apps log"</langsyntaxhighlight>
{{Out}}
<pre>>EventLog.BAT
Line 237 ⟶ 246:
></pre>
If you do not want the command to display its result or errors...
<langsyntaxhighlight lang="dos">@echo off
EventCreate /t ERROR /id 123 /l SYSTEM /so "A Batch File" /d "This is found in system log." >NUL 2>&1
EventCreate /t WARNING /id 456 /l APPLICATION /so BlaBla /d "This is found in apps log" >NUL 2>&1
::That ">NUL 2>&1" trick actually works in any command!</langsyntaxhighlight>
 
'''NOTE:''' This will (...or might) not work if you do not have administrator privileges.
Line 247 ⟶ 256:
{{works with|BBC BASIC for Windows}}
Writes to the Application Log:
<langsyntaxhighlight lang="bbcbasic"> INSTALL @lib$+"COMLIB"
PROC_cominitlcid(1033)
Line 254 ⟶ 263:
PROC_releaseobject(WshShell%)
PROC_comexit</langsyntaxhighlight>
 
=={{header|C}}==
The following is a wrapper on the EventCreate utility provided in Windows. Note that to use this wrapper, the code must be executed from a console/IDE running as Administrator. The utility itself does extensive error-checking and validation, so apart from the check that 5 arguments have been supplied, no other validations or checks are performed.
<syntaxhighlight lang="c">
<lang C>
#include<stdlib.h>
#include<stdio.h>
Line 275 ⟶ 284:
return 0;
}
</syntaxhighlight>
</lang>
Invocation and output on console :
<pre>
Line 286 ⟶ 295:
=={{header|C sharp}}==
In Windows Vista and later or Windows Server 2003, you must have administrative privileges to execute this code.
<langsyntaxhighlight lang="csharp">using System.Diagnostics;
 
namespace RC
Line 305 ⟶ 314:
}
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
{{trans|C}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <sstream>
 
Line 329 ⟶ 338:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(use 'clojure.java.shell)
(sh "eventcreate" "/T" "INFORMATION" "/ID" "123" "/D" "Rosetta Code example")</langsyntaxhighlight>
 
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight Dlang="d">import std.process;
import std.stdio;
 
Line 348 ⟶ 357:
writeln("Failed to execute command, status=", cmd.status);
}
}</langsyntaxhighlight>
 
=={{header|Delphi}}==
<langsyntaxhighlight Delphilang="delphi">program WriteToEventLog;
 
{$APPTYPE CONSOLE}
Line 376 ⟶ 385:
begin
WriteLog('Message to log.');
end.</langsyntaxhighlight>
 
=={{header|F_Sharp|F#}}==
<p>Bare bone writing to the Application Eventlog giving no event-ID and using the default event type (information.)</p>
<langsyntaxhighlight lang="fsharp">use log = new System.Diagnostics.EventLog()
log.Source <- "Sample Application"
log.WriteEntry("Entered something in the Application Eventlog!")</langsyntaxhighlight>
 
=={{header|Go}}==
This works on Windows 10 with administrative privileges.
<langsyntaxhighlight lang="go">package main
 
import (
Line 402 ⟶ 411:
fmt.Println(err)
}
}</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
Line 433 ⟶ 442:
}
}
}</langsyntaxhighlight>
 
=={{header|Julia}}==
Run as an administrator.
<langsyntaxhighlight lang="julia">
cmd = "eventcreate /T INFORMATION /ID 123 /D \"Rosetta Code Write to Windows event log task example\""
Base.run(`$cmd`)
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
The following works on Windows 10 with administrative privileges:
<langsyntaxhighlight lang="scala">// version 1.1.4-3
 
fun main(args: Array<String>) {
Line 455 ⟶ 464:
 
Runtime.getRuntime().exec(command)
}</langsyntaxhighlight>
 
=={{header|Lingo}}==
{{libheader|Shell xtra}}
<langsyntaxhighlight Lingolang="lingo">shell = xtra("Shell").new()
props = [:]
props["operation"] = "runas"
props["parameters"] = "/t INFORMATION /id 123 /l APPLICATION /so Lingo /d "&QUOTE&"Rosetta Code Example"&QUOTE
shell.shell_exec("EventCreate", props)</langsyntaxhighlight>
 
=={{header|Perl}}==
Line 469 ⟶ 478:
The Win32::EventLog module has the Report method to write in the EventLog
 
<langsyntaxhighlight lang="perl">
use strict;
use warnings;
Line 486 ⟶ 495:
};
$handle->Report($event);
</syntaxhighlight>
</lang>
 
=={{header|Phix}}==
The first two lines are of course entirely optional, but could help prevent someone/a newbie from wasting their time trying to achieve the impossible.
<lang Phix>system(`eventcreate /T INFORMATION /ID 123 /D "Rosetta Code Write to Windows event log task example"`)</lang>
<!--<syntaxhighlight lang="phix">(notonline)-->
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #004600;">WINDOWS</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (as in this will not work on Linux or p2js, duh)</span>
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (as above, also prevent pointless attempts to transpile)</span>
<langspan Phixstyle="color: #7060A8;">system</span><span style="color: #0000FF;">(</span><span style="color: #008000;">`eventcreate /T INFORMATION /ID 123 /D "Rosetta Code Write to Windows event log task example"`</span><span style="color: #0000FF;">)</langspan>
<!--</syntaxhighlight>-->
{{out}} (when running as administrator)
<pre>
Line 497 ⟶ 511:
=={{header|PicoLisp}}==
PicoLisp doesn't run on Windows. In case of Linux, the equivalent of the event log is the syslog. It can be written with '[http://software-lab.de/doc/refN.html#native native]' C functions, or simply with the 'logger' utility:
<langsyntaxhighlight PicoLisplang="picolisp">: (call 'logger "This is a test")
-> T
 
: (call 'logger "This" 'is "another" 'test)
-> T</langsyntaxhighlight>
 
=={{header|PowerShell}}==
<langsyntaxhighlight lang="powershell"># Create Event Log object
$EventLog=new-object System.Diagnostics.EventLog("Application")
#Declare Event Source; must be 'registered' with Windows
Line 516 ⟶ 530:
 
# Write the event in the format "Event test",EventType,EventID
$EventLog.WriteEntry("My Test Event",$infoevent,70)</langsyntaxhighlight>
''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>
Line 522 ⟶ 536:
 
===Source and event log existing===
<langsyntaxhighlight lang="powershell">
$MessageFreeLula = 'Global unions and union leaders from more than 50 countries came together ' +
'in Geneva today to stand in solidarity with former Brazilian President Lula, calling for ' +
Line 528 ⟶ 542:
Write-EventLog -LogName 'System' -Source 'Eventlog' -Message $MessageFreeLula -EventId 13 -EntryType 'Information'
'SUCCESS: The Lula Livre message (#FreeLula) has been recorded in the system log event.'
</syntaxhighlight>
</lang>
{{out|output}}
<pre>
Line 534 ⟶ 548:
</pre>
===New event log===
<langsyntaxhighlight lang="powershell">
$MessageFreeLula = 'Global unions and union leaders from more than 50 countries came together ' +
'in Geneva today to stand in solidarity with former Brazilian President Lula, calling for ' +
Line 542 ⟶ 556:
Write-EventLog -LogName 'Free Lula!' -Source '#FreeLula' -Message $MessageFreeLula -EventId 13 -EntryType 'Information'
'SUCCESS: The Lula Livre message (#FreeLula) has been recorded in the "Free Lula!" log event.'
</syntaxhighlight>
</lang>
{{out|output}}
<pre>
Line 549 ⟶ 563:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Procedure WriteToLog(Event_App$,EventMessage$,EvenetType,Computer$)
 
Protected wNumStrings.w, lpString=@EventMessage$, lReturnX, CMessageTyp, lparray
Line 562 ⟶ 576:
 
ProcedureReturn Result
EndProcedure</langsyntaxhighlight>
 
=={{header|Python}}==
Line 568 ⟶ 582:
{{libheader|PyWin32}}
 
<langsyntaxhighlight Pythonlang="python">import win32api
import win32con
import win32evtlog
Line 586 ⟶ 600:
 
win32evtlogutil.ReportEvent(applicationName, eventID, eventCategory=category,
eventType=myType, strings=descr, data=data, sid=my_sid)</langsyntaxhighlight>
 
=={{header|Racket}}==
Racket's logging facility creates windows events when running on Windows.
 
<syntaxhighlight lang="racket">
<lang Racket>
#lang racket
(log-warning "Warning: nothing went wrong.")
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 603 ⟶ 617:
(Same caveats as the others, needs to be run as administrator or with elevated privileges under Windows.)
 
<syntaxhighlight lang="raku" perl6line>given $*DISTRO {
when .is-win {
my $cmd = "eventcreate /T INFORMATION /ID 123 /D \"Bla de bla bla bla\"";
Line 613 ⟶ 627:
$logger.info("[$*PROGRAM-NAME pid=$*PID user=$*USER] Just thought you might like to know.");
}
}</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 619 ⟶ 633:
 
===annotated===
<langsyntaxhighlight lang="rexx">/*REXX program writes a "record" (event) to the (Microsoft) Windows event log. */
 
eCMD = 'EVENTCREATE' /*name of the command that'll be used. */
Line 631 ⟶ 645:
eCMD '/T' type "/ID" id '/L' logName "/SO" source '/D' desc
 
/*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output}}
<pre>
Line 638 ⟶ 652:
 
===bare bones===
<langsyntaxhighlight lang="rexx">/*REXX program writes a "record" (event) to the (Microsoft) Windows event log. */
/* [↓] cmd options have extra spacing.*/
 
Line 644 ⟶ 658:
'/D "attempting to add an entry for a Rosetta Code demonstration."'
 
/*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version.}} <br><br>
 
=={{header|Ruby}}==
{{libheader|win32-utils}}
<langsyntaxhighlight lang="ruby">require 'win32/eventlog'
logger = Win32::EventLog.new
logger.report_event(:event_type => Win32::EventLog::INFO, :data => "a test event log entry")</langsyntaxhighlight>
 
Instructions on setting up an Event Source is [http://rubyforge.org/docman/view.php/85/1734/mc_tutorial.html here]
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
#[cfg(windows)]
mod bindings {
Line 735 ⟶ 749:
println!("Not implemented");
}
</syntaxhighlight>
</lang>
add this to the build.rs:
<langsyntaxhighlight lang="rust">
fn main() {
#[cfg(windows)]
Line 745 ⟶ 759:
}
}
</syntaxhighlight>
</lang>
And this to cargo.toml:
<langsyntaxhighlight lang="rust">
[target.'cfg(windows)'.dependencies]
windows = "0.7.0"
Line 753 ⟶ 767:
[target.'cfg(windows)'.build-dependencies]
windows = "0.7.0"
</syntaxhighlight>
</lang>
 
=={{header|Scala}}==
The following works on Windows 10 with elevated (administrative) permission:
<langsyntaxhighlight Scalalang="scala">object RegisterWinLogEvent extends App {
 
import sys.process._
Line 767 ⟶ 781:
println(s"\nSuccessfully completed without errors. [total ${scala.compat.Platform.currentTime - executionStart} ms]")
 
}</langsyntaxhighlight>
 
=={{header|Standard ML}}==
From bsd/linux/unix to log
<syntaxhighlight lang="standard ml">
<lang Standard ML>
OS.Process.system "logger \"Log event\" " ;
</syntaxhighlight>
</lang>
From MS Windows to MS Windows, taken from C:
<syntaxhighlight lang="standard ml">
<lang Standard ML>
OS.Process.system "EventCreate /t WARNING /id 458 /l APPLICATION /so SomeSource /d \"Settastring\"" ;
</syntaxhighlight>
</lang>
 
=={{header|Tcl}}==
{{libheader|TWAPI}}
<langsyntaxhighlight 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</langsyntaxhighlight>
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
<lang vb>
Sub write_event(event_type,msg)
Set objShell = CreateObject("WScript.Shell")
Line 809 ⟶ 823:
 
Call write_event("INFORMATION","This is a test information.")
</syntaxhighlight>
</lang>
 
=={{header|Wren}}==
{{trans|Go}}
This embedded program is untested as I no longer have a working Windows machine but should work when run with administrative privileges - what can possibly go wrong?
<langsyntaxhighlight ecmascriptlang="wren">/* write_to_windows_event_logWrite_to_Windows_event_log.wren */
 
class Windows {
Line 825 ⟶ 839:
].join(" ")
 
Windows.eventCreate(args)</langsyntaxhighlight>
<br>
Now embed this script in the following C program, compile and run it.
<syntaxhighlight lang="c">/* gcc Write_to_Windows_event_log.c -o Write_to_Windows_event_log -lwren -lm */
<lang c>#include <stdio.h>
 
<lang c>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 895 ⟶ 911:
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "write_to_windows_event_logWrite_to_Windows_event_log.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
Line 911 ⟶ 927:
free(script);
return 0;
}</langsyntaxhighlight>
 
=={{header|zkl}}==
{{trans|Clojure}}
<langsyntaxhighlight lang="zkl">zkl: System.cmd(0'|eventcreate "/T" "INFORMATION" "/ID" "123" "/D" "Rosetta Code example"|)</langsyntaxhighlight>
{{out}}
<pre>
Line 921 ⟶ 937:
0
</pre>
{{omit from|6502 Assembly|Windows has never been made for this hardware (not that I know of anyway)}}
 
{{omit from|68000 Assembly|Windows has never been made for this hardware (not that I know of anyway)}}
{{omit from|Lotus 123 Macro Scripting|Would overwrite the log file}}
{{omit from|Mathematica}}
{{omit from|Maxima}}
{{omit from|MIPS Assembly|Windows has never been made for this hardware (not that I know of anyway)}}
{{omit from|PARI/GP}}
{{omit from|PostScript}}
{{omit from|Retro|No access to Windows APIs}}
{{omit from|TI-83 BASIC|Cannot run on Windows}}
{{omit from|Z80 Assembly|Windows has never been made for this hardware (not that I know of anyway)}}
{{omit from|zkl}}
{{omit from|ZX Spectrum Basic|Microdrive access is slow, and there is no real time clock, so event logging is not usually done}}
9,476

edits