Write to Windows event log: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (→{{header|Perl 6}}: Add a Perl 6 example) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 196: | Line 196: | ||
return char_count |
return char_count |
||
}</lang> |
}</lang> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<lang AWK> |
<lang AWK> |
||
Line 282: | Line 283: | ||
</pre> |
</pre> |
||
Microsoft does provide an C/C++ API for EventCreate, but as with everything Microsoft, it's so wonderfully convoluted, that I will just give a link to the [https://msdn.microsoft.com/en-us/library/aa363680(v=vs.85).aspx ReportEvent] example. |
Microsoft does provide an C/C++ API for EventCreate, but as with everything Microsoft, it's so wonderfully convoluted, that I will just give a link to the [https://msdn.microsoft.com/en-us/library/aa363680(v=vs.85).aspx ReportEvent] example. |
||
=={{header|C sharp}}== |
|||
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> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
Line 305: | Line 329: | ||
return 0; |
return 0; |
||
}</lang> |
|||
=={{header|C sharp}}== |
|||
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> |
}</lang> |
||
Line 440: | Line 441: | ||
Base.run(`$cmd`) |
Base.run(`$cmd`) |
||
</lang> |
</lang> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
Line 487: | Line 487: | ||
$handle->Report($event); |
$handle->Report($event); |
||
</lang> |
</lang> |
||
=={{header|Perl 6}}== |
|||
{{works with|Rakudo|2020.01}} |
|||
There is not yet (that I am aware of) a native interface to the Windows logging functions, but Perl 6 can shell out and run a console command just as easily as most of these other languages. It ''does'' have a native interface to the syslog functions under POSIX environments though. |
|||
(Same caveats as the others, needs to be run as administrator or with elevated privileges under Windows.) |
|||
<lang perl6>given $*DISTRO { |
|||
when .is-win { |
|||
my $cmd = "eventcreate /T INFORMATION /ID 123 /D \"Bla de bla bla bla\""; |
|||
run($cmd); |
|||
} |
|||
default { # most POSIX environments |
|||
use Log::Syslog::Native; |
|||
my $logger = Log::Syslog::Native.new(facility => Log::Syslog::Native::User); |
|||
$logger.info("[$*PROGRAM-NAME pid=$*PID user=$*USER] Just thought you might like to know."); |
|||
} |
|||
}</lang> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 521: | Line 502: | ||
: (call 'logger "This" 'is "another" 'test) |
: (call 'logger "This" 'is "another" 'test) |
||
-> T</lang> |
-> T</lang> |
||
=={{header|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> |
|||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
Line 583: | Line 548: | ||
</pre> |
</pre> |
||
=={{header|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> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
Line 615: | Line 595: | ||
(log-warning "Warning: nothing went wrong.") |
(log-warning "Warning: nothing went wrong.") |
||
</lang> |
</lang> |
||
=={{header|Scala}}== |
|||
The following works on Windows 10 with elevated (administrative) permission: |
|||
<lang Scala>object RegisterWinLogEvent extends App { |
|||
=={{header|Raku}}== |
|||
import sys.process._ |
|||
(formerly Perl 6) |
|||
{{works with|Rakudo|2020.01}} |
|||
There is not yet (that I am aware of) a native interface to the Windows logging functions, but Perl 6 can shell out and run a console command just as easily as most of these other languages. It ''does'' have a native interface to the syslog functions under POSIX environments though. |
|||
(Same caveats as the others, needs to be run as administrator or with elevated privileges under Windows.) |
|||
def eventCreate= Seq("EVENTCREATE", "/T", "SUCCESS", "/id", "123", "/l", "APPLICATION", "/so", "Scala RegisterWinLogEvent", "/d", "Rosetta Code Example" ).!! |
|||
println(eventCreate) |
|||
println(s"\nSuccessfully completed without errors. [total ${scala.compat.Platform.currentTime - executionStart} ms]") |
|||
<lang perl6>given $*DISTRO { |
|||
when .is-win { |
|||
my $cmd = "eventcreate /T INFORMATION /ID 123 /D \"Bla de bla bla bla\""; |
|||
run($cmd); |
|||
} |
|||
default { # most POSIX environments |
|||
use Log::Syslog::Native; |
|||
my $logger = Log::Syslog::Native.new(facility => Log::Syslog::Native::User); |
|||
$logger.info("[$*PROGRAM-NAME pid=$*PID user=$*USER] Just thought you might like to know."); |
|||
} |
|||
}</lang> |
}</lang> |
||
Line 668: | Line 654: | ||
Instructions on setting up an Event Source is [http://rubyforge.org/docman/view.php/85/1734/mc_tutorial.html here] |
Instructions on setting up an Event Source is [http://rubyforge.org/docman/view.php/85/1734/mc_tutorial.html here] |
||
=={{header|Scala}}== |
|||
The following works on Windows 10 with elevated (administrative) permission: |
|||
<lang Scala>object RegisterWinLogEvent extends App { |
|||
import sys.process._ |
|||
def eventCreate= Seq("EVENTCREATE", "/T", "SUCCESS", "/id", "123", "/l", "APPLICATION", "/so", "Scala RegisterWinLogEvent", "/d", "Rosetta Code Example" ).!! |
|||
println(eventCreate) |
|||
println(s"\nSuccessfully completed without errors. [total ${scala.compat.Platform.currentTime - executionStart} ms]") |
|||
}</lang> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |