Write to Windows event log: Difference between revisions
m
syntax highlighting fixup automation
Puppydrum64 (talk | contribs) mNo edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 8:
{{trans|C}}
<
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]))</
=={{header|AutoHotkey}}==
<
h := RegisterForEvents("AutoHotkey")
SendWinLogEvent(h, "Test Message")
Line 204:
; Return the number of characters copied.
return char_count
}</
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f WRITE_TO_WINDOWS_EVENT_LOG.AWK
BEGIN {
Line 226:
}
function error(message) { printf("error: %s\n",message) ; errors++ }
</syntaxhighlight>
{{out}}
<pre>
Line 234:
=={{header|Batch File}}==
The "EventCreate" command does the task.
<
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"</
{{Out}}
<pre>>EventLog.BAT
Line 246:
></pre>
If you do not want the command to display its result or errors...
<
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!</
'''NOTE:''' This will (...or might) not work if you do not have administrator privileges.
Line 256:
{{works with|BBC BASIC for Windows}}
Writes to the Application Log:
<
PROC_cominitlcid(1033)
Line 263:
PROC_releaseobject(WshShell%)
PROC_comexit</
=={{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">
#include<stdlib.h>
#include<stdio.h>
Line 284:
return 0;
}
</syntaxhighlight>
Invocation and output on console :
<pre>
Line 295:
=={{header|C sharp}}==
In Windows Vista and later or Windows Server 2003, you must have administrative privileges to execute this code.
<
namespace RC
Line 314:
}
}
}</
=={{header|C++}}==
{{trans|C}}
<
#include <sstream>
Line 338:
return 0;
}</
=={{header|Clojure}}==
<
(sh "eventcreate" "/T" "INFORMATION" "/ID" "123" "/D" "Rosetta Code example")</
=={{header|D}}==
{{trans|Kotlin}}
<
import std.stdio;
Line 357:
writeln("Failed to execute command, status=", cmd.status);
}
}</
=={{header|Delphi}}==
<
{$APPTYPE CONSOLE}
Line 385:
begin
WriteLog('Message to log.');
end.</
=={{header|F_Sharp|F#}}==
<p>Bare bone writing to the Application Eventlog giving no event-ID and using the default event type (information.)</p>
<
log.Source <- "Sample Application"
log.WriteEntry("Entered something in the Application Eventlog!")</
=={{header|Go}}==
This works on Windows 10 with administrative privileges.
<
import (
Line 411:
fmt.Println(err)
}
}</
=={{header|Java}}==
<
import java.io.IOException;
import java.io.InputStream;
Line 442:
}
}
}</
=={{header|Julia}}==
Run as an administrator.
<
cmd = "eventcreate /T INFORMATION /ID 123 /D \"Rosetta Code Write to Windows event log task example\""
Base.run(`$cmd`)
</syntaxhighlight>
=={{header|Kotlin}}==
The following works on Windows 10 with administrative privileges:
<
fun main(args: Array<String>) {
Line 464:
Runtime.getRuntime().exec(command)
}</
=={{header|Lingo}}==
{{libheader|Shell xtra}}
<
props = [:]
props["operation"] = "runas"
props["parameters"] = "/t INFORMATION /id 123 /l APPLICATION /so Lingo /d ""E&"Rosetta Code Example""E
shell.shell_exec("EventCreate", props)</
=={{header|Perl}}==
Line 478:
The Win32::EventLog module has the Report method to write in the EventLog
<
use strict;
use warnings;
Line 495:
};
$handle->Report($event);
</syntaxhighlight>
=={{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.
<!--<
<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>
<span style="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;">)</span>
<!--</
{{out}} (when running as administrator)
<pre>
Line 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:
<
-> T
: (call 'logger "This" 'is "another" 'test)
-> T</
=={{header|PowerShell}}==
<
$EventLog=new-object System.Diagnostics.EventLog("Application")
#Declare Event Source; must be 'registered' with Windows
Line 530:
# Write the event in the format "Event test",EventType,EventID
$EventLog.WriteEntry("My Test Event",$infoevent,70)</
''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 536:
===Source and event log existing===
<
$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:
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>
{{out|output}}
<pre>
Line 548:
</pre>
===New event log===
<
$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 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>
{{out|output}}
<pre>
Line 563:
=={{header|PureBasic}}==
<
Protected wNumStrings.w, lpString=@EventMessage$, lReturnX, CMessageTyp, lparray
Line 576:
ProcedureReturn Result
EndProcedure</
=={{header|Python}}==
Line 582:
{{libheader|PyWin32}}
<
import win32con
import win32evtlog
Line 600:
win32evtlogutil.ReportEvent(applicationName, eventID, eventCategory=category,
eventType=myType, strings=descr, data=data, sid=my_sid)</
=={{header|Racket}}==
Racket's logging facility creates windows events when running on Windows.
<syntaxhighlight lang="racket">
#lang racket
(log-warning "Warning: nothing went wrong.")
</syntaxhighlight>
=={{header|Raku}}==
Line 617:
(Same caveats as the others, needs to be run as administrator or with elevated privileges under Windows.)
<syntaxhighlight lang="raku"
when .is-win {
my $cmd = "eventcreate /T INFORMATION /ID 123 /D \"Bla de bla bla bla\"";
Line 627:
$logger.info("[$*PROGRAM-NAME pid=$*PID user=$*USER] Just thought you might like to know.");
}
}</
=={{header|REXX}}==
Line 633:
===annotated===
<
eCMD = 'EVENTCREATE' /*name of the command that'll be used. */
Line 645:
eCMD '/T' type "/ID" id '/L' logName "/SO" source '/D' desc
/*stick a fork in it, we're all done. */</
{{out|output}}
<pre>
Line 652:
===bare bones===
<
/* [↓] cmd options have extra spacing.*/
Line 658:
'/D "attempting to add an entry for a Rosetta Code demonstration."'
/*stick a fork in it, we're all done. */</
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}} <br><br>
=={{header|Ruby}}==
{{libheader|win32-utils}}
<
logger = Win32::EventLog.new
logger.report_event(:event_type => Win32::EventLog::INFO, :data => "a test event log entry")</
Instructions on setting up an Event Source is [http://rubyforge.org/docman/view.php/85/1734/mc_tutorial.html here]
=={{header|Rust}}==
<
#[cfg(windows)]
mod bindings {
Line 749:
println!("Not implemented");
}
</syntaxhighlight>
add this to the build.rs:
<
fn main() {
#[cfg(windows)]
Line 759:
}
}
</syntaxhighlight>
And this to cargo.toml:
<
[target.'cfg(windows)'.dependencies]
windows = "0.7.0"
Line 767:
[target.'cfg(windows)'.build-dependencies]
windows = "0.7.0"
</syntaxhighlight>
=={{header|Scala}}==
The following works on Windows 10 with elevated (administrative) permission:
<
import sys.process._
Line 781:
println(s"\nSuccessfully completed without errors. [total ${scala.compat.Platform.currentTime - executionStart} ms]")
}</
=={{header|Standard ML}}==
From bsd/linux/unix to log
<syntaxhighlight lang="standard ml">
OS.Process.system "logger \"Log event\" " ;
</syntaxhighlight>
From MS Windows to MS Windows, taken from C:
<syntaxhighlight lang="standard ml">
OS.Process.system "EventCreate /t WARNING /id 458 /l APPLICATION /so SomeSource /d \"Settastring\"" ;
</syntaxhighlight>
=={{header|Tcl}}==
{{libheader|TWAPI}}
<
# This command handles everything; use “-type error” to write an error message
twapi::eventlog_log "My Test Event" -type info</
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
Sub write_event(event_type,msg)
Set objShell = CreateObject("WScript.Shell")
Line 823:
Call write_event("INFORMATION","This is a test information.")
</syntaxhighlight>
=={{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?
<
class Windows {
Line 839:
].join(" ")
Windows.eventCreate(args)</
<br>
Now embed this script in the following C program, compile and run it.
<
#include <stdlib.h>
#include <string.h>
Line 925:
free(script);
return 0;
}</
=={{header|zkl}}==
{{trans|Clojure}}
<
{{out}}
<pre>
|