Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Write to WIndows Event Log

by sungy (Initiate)
on Sep 09, 2005 at 19:59 UTC ( [id://490731]=perlquestion: print w/replies, xml ) Need Help??

sungy has asked for the wisdom of the Perl Monks concerning the following question:

I am new to perl and I had a problem in writing to the Windows Event Log from my program. Actually after the program run, an entry had been written to the event log but when I view it in the Event Viewer, it show mine the following : The description for Event ID ( 0 ) in Source ( MyProgram ) cannot be found. The local computer may not have the necessary registry information or message DLL files to display messages from a remote computer. You may be able to use the /AUXSOURCE= flag to retrieve this description; see Help and Support for details. The following information is part of the event: Test. Here is my code
sub writeeventlog { use Win32::EventLog; my $eventLog = Win32::EventLog->new('MyProgram') my %eventRecord = ( 'Source' => 'MyProgram', 'Computer' => 0, 'Length' => 0, 'RecordNumber' => 0, 'TimeGenerated' => 0, 'Timewritten' => 0, 'ClosingReocrdNumber' => 0, 'Category' => NULL, 'EventID' => 0, 'EventType' => EVENTLOG_ERROR_TYPE, 'Strings' => 'Test.' 'Data' => 'Test.', ); $eventLog->Report(\%eventRecord); $eventLog->Close(); }
Also I realized that the source is not register in the registry under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\Application\ Please help and how I can register the event source, many thanks.

Replies are listed 'Best First'.
Re: Write to WIndows Event Log
by NetWallah (Canon) on Sep 09, 2005 at 20:49 UTC
    I have syntax-corrected your snippet (Missing semi-colon, comma, bogus NULL), and removed non-existant noise parameters.
    use strict; writeeventlog(); ###################################### sub writeeventlog { use Win32::EventLog; my $eventLog = Win32::EventLog->new('MyProgram'); my %eventRecord = ( #'Source' => undef, #'Computer' => undef, 'Category' => 0, #NULL, 'EventID' => 0, 'EventType' => EVENTLOG_ERROR_TYPE, 'Strings' => 'Test.', 'Data' => 'Test.', ); $eventLog->Report(\%eventRecord); $eventLog->Close(); }
    This writes entries to the event log just fine. The source does NOT haveto be registered in the Registry, but you can follow shonorio's advice.

    The Eventviewer message is an attempt to decode the EventID, but since you do not have a message file or DLL associated with your app, you will need to ignore the Event viewers failed attempt to provide a "description" for your eventID.

         "Man cannot live by bread alone...
             He'd better have some goat cheese and wine to go with it!"

      you will need to ignore the Event viewers failed attempt to provide a "description" for your eventID...

      Without that information the entire message is of limited value. The cleaned up code does work though. Oh, I get it. The intended message is getting appended to the end of the weirdo system message that tells you that your app. is not registered. I would hate to be the guy explaining that to a System's Adminstrator...

      Celebrate Intellectual Diversity

Re: Write to WIndows Event Log
by InfiniteSilence (Curate) on Sep 09, 2005 at 20:14 UTC
    Hey, the POD of Win32::EventLog show how you can READ from the Event Viewer, not write to it. Perhaps I read your question incorrectly? In which case I apologize, but you can write to it very easily:
    #!/usr/bin/perl -w use strict; use Win32::OLE; my ($app)=Win32::OLE->new("WScript.Shell"); $app->LogEvent(4,"You know it man!"); 1;

    Celebrate Intellectual Diversity

Re: Write to WIndows Event Log
by shonorio (Hermit) on Sep 09, 2005 at 20:38 UTC
    You are getting this message because Eventlog Viewer are not find the dll associate with the source of your event.

    Take a look on "Example of how to build a message dll", where show how can you create a eventlog dll. With this dll, you can registry on Eventlog and get the correct message.

    Win32::EventLog::Message could be usefull too.

    Solli Moreira Honorio
    Sao Paulo - Brazil
Re: Write to WIndows Event Log
by jdporter (Paladin) on Mar 14, 2006 at 13:03 UTC

    The above are all excellent answers. For the sake of those who may be using cygwin without Win32::EventLog, here's a solution that uses a command-line utility, syslog.exe, that comes with cygwin.

    system qq( /bin/syslog.exe -p info -t "Prog" "The message." );

    The argument of the -t option appears in the 'Source' column of the event viewer. If omitted, it defaults to the executing user's login name.

    The message goes into the 'Description' field of the event.

    This method has the same issue with programs not being registered, as noted in the answers above.

    We're building the house of the future together.
Re: Write to WIndows Event Log
by wilsond (Scribe) on Jan 21, 2009 at 08:27 UTC

    In case it helps someone, here's how I did it: [fixed] Write to Win32 Event Log, get "/AUXSOURCE=" error


    While I ask a lot of Win32 questions, I hate Windows with a passion. That's the problem with writing a cross-platform program. I'm a Linux user myself. I wish more people were.
    If you want to do evil, science provides the most powerful weapons to do evil; but equally, if you want to do good, science puts into your hands the most powerful tools to do so.
    - Richard Dawkins

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://490731]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-03-28 17:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found