Write to Windows event log: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(→‎{{header|Perl 6}}: Add a Perl 6 example)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 196:
return char_count
}</lang>
 
=={{header|AWK}}==
<lang AWK>
Line 282 ⟶ 283:
</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.
 
=={{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++}}==
Line 305 ⟶ 329:
 
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>
 
Line 440 ⟶ 441:
Base.run(`$cmd`)
</lang>
 
 
=={{header|Kotlin}}==
Line 487:
$handle->Report($event);
</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}}==
Line 521 ⟶ 502:
: (call 'logger "This" 'is "another" 'test)
-> 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}}==
Line 583 ⟶ 548:
</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}}==
Line 615 ⟶ 595:
(log-warning "Warning: nothing went wrong.")
</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>
 
Line 668 ⟶ 654:
 
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}}==
10,333

edits