{"id":1538,"date":"2013-02-13T17:32:09","date_gmt":"2013-02-13T22:32:09","guid":{"rendered":"http:\/\/anvil-of-time.com\/wordpress\/?p=1538"},"modified":"2021-03-11T12:55:51","modified_gmt":"2021-03-11T17:55:51","slug":"powerbuilder-writing-to-the-windows-event-log","status":"publish","type":"post","link":"https:\/\/anvil-of-time.com\/powerbuilder\/powerbuilder-writing-to-the-windows-event-log\/","title":{"rendered":"PowerBuilder &#8211; Writing to the Windows Event Log"},"content":{"rendered":"<p>Here are \u00a0two ways to write to the Event log in Windows and indicate the source of the message.<\/p>\n<p><strong>Reportevent<\/strong> External Function and <strong>Eventcreate<\/strong> command line command<\/p>\n<p>You can use the command shell to do this too but\u00a0this has the drawback of not being able to assign the source of the event.<\/p>\n<p>First you need the following External Functions:<\/p>\n<pre class=\"brush: vb; gutter: true\">Function ulong RegisterEventSource ( &amp;\n\tulong lpUNCServerName, &amp;\n\tstring lpSourceName &amp;\n\t) Library &quot;advapi32.dll&quot; Alias For &quot;RegisterEventSourceW&quot;\n\nFunction boolean ReportEvent ( &amp;\n\tulong hEventLog, &amp;\n\tuint wType, &amp;\n\tuint wCategory, &amp;\n\tulong dwEventID, &amp;\n\tulong lpUserSid, &amp;\n\tuint wNumStrings, &amp;\n\tulong dwDataSize, &amp;\n\tstring lpStrings[], &amp;\n\tulong lpRawData &amp;\n\t) Library &quot;advapi32.dll&quot; Alias For &quot;ReportEventW&quot;\n\nFunction boolean DeregisterEventSource ( &amp;\n\tref ulong hEventLog &amp;\n\t) Library &quot;advapi32.dll&quot;<\/pre>\n<p>Then in PowerScript<\/p>\n<pre class=\"brush: vb; gutter: true\">integer li_messagelevel\nstring ls_errmsg[]\nstring ls_messagesource\nulong lul_eventsource\n\/\/ the event message is here\nls_errmsg[1] = &quot;Event Log Entry &quot; + String(now(), &#039;hh:mm:ss&#039;) \n\/\/ setting the source of the message\nls_messagesource = this.classname()\n\nlul_eventsource = RegisterEventSource(0, ls_messagesource)\nIF IsNull(ls_eventsource) THEN ls_eventsource = -1\n\/\/ set the level of the message\nli_messagelevel = 4 \/\/ Information,  also 1 = Error, 2 = Warning\n\nIF lul_eventsource &gt; 0 THEN\n\t\/\/ write to the log\n\tReportEvent(lul_eventsource, li_messagelevel, 0, 0, 0, &amp;\n        UpperBound(ls_errmsg), 0, ls_errmsg, 0)\n\tDeregisterEventSource(lul_eventsource)\nEND IF<\/pre>\n<p>Viewing the Log<\/p>\n<p><a href=\"http:\/\/anvil-of-time.com\/wordpress\/wp-content\/uploads\/2013\/02\/pbeventlog.png\"><img loading=\"lazy\" class=\"aligncenter wp-image-1539 size-full\" src=\"https:\/\/anvil-of-time.com\/wp-content\/uploads\/2013\/02\/pbeventlog.png\" alt=\"pbeventlog\" width=\"860\" height=\"700\" srcset=\"https:\/\/anvil-of-time.com\/wp-content\/uploads\/2013\/02\/pbeventlog.png 860w, https:\/\/anvil-of-time.com\/wp-content\/uploads\/2013\/02\/pbeventlog-300x244.png 300w, https:\/\/anvil-of-time.com\/wp-content\/uploads\/2013\/02\/pbeventlog-150x122.png 150w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><\/a><\/p>\n<p>Per the MSDN documentation, if the source name cannot be found the event logging service uses the Application log. Although events will be reported , the events will not include descriptions because there are no message and category message files for looking up descriptions related to the event identifiers.<\/p>\n<p>Thanks to Roland Smith for most of the code for this functionality.<\/p>\n<p><strong>You are also able to use the &#8216;EVENTCREATE&#8217; command line command<\/strong>.<\/p>\n<p>From the Microsoft Technet page (found<a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/bb490899.aspx\" target=\"_blank\" rel=\"noopener\"> Here<\/a>):<\/p>\n<pre class=\"brush: text; gutter: false\">Enables an administrator to create a custom event in a specified event log.\nSyntax\neventcreate [\/s Computer [\/u Domain\\User [\/p Password]] {[\/l {APPLICATION|SYSTEM}]|[\/so SrcName]} \/t {ERROR|WARNING|INFORMATION|SUCCESSAUDIT|FAILUREAUDIT} \/id EventID \/d Description\nTop of page \nParameters\n\/s   Computer   : Specifies the name or IP address of a remote computer (do not use backslashes). The default is the local computer.\n\/u   Domain \\ User   : Runs the command with the account permissions of the user specified by User or Domain\\User. The default is the permissions of the current logged on user on the computer issuing the command.\n\/p   Password   : Specifies the password of the user account that is specified in the \/u parameter.\n\/l { APPLICATION | SYSTEM } : Specifies the name of the event log where the event will be created. The valid log names are APPLICATION and SYSTEM.\n\/so   SrcName   : Specifies the source to use for the event. A valid source can be any string and should represent the application or component that is generating the event.\n\/t { ERROR | WARNING | INFORMATION | SUCCESSAUDIT | FAILUREAUDIT } : Specifies the type of event to create. The valid types are ERROR, WARNING, INFORMATION, SUCCESSAUDIT, and FAILUREAUDIT.\n\/id   EventID   : Specifies the event ID for the event. A valid ID is any number from 1 to 65535.\n\/d   Description   : Specifies the description to use for the newly created event.\n\/? : Displays help at the command prompt.<\/pre>\n<p>In PowerScript:<\/p>\n<pre class=\"brush: vb; gutter: true\">\/\/ command line event log\ninteger li_rtn\nstring ls_msg, ls_eventmsg\nls_msg = &#039;Command Line Message&#039;\n\n ls_EventMsg = &#039;EVENTCREATE \/T ERROR \/ID 100 \/SO &quot;My Application&quot; \/l APPLICATION \/D &quot;&#039; +ls_msg + &#039;&quot; &#039;\n li_Rtn = Run (ls_EventMsg,  Minimized!)\n\n IF li_Rtn &lt;&gt; 1 THEN\n\tmessagebox(&#039;event log&#039;, &#039;error loging msg&#039;)\n END IF<\/pre>\n<p>And the message in the Event Log viewer<\/p>\n<p><a href=\"http:\/\/anvil-of-time.com\/wordpress\/wp-content\/uploads\/2013\/02\/eventcreatemessage.png\"><img loading=\"lazy\" class=\"aligncenter wp-image-1548 size-full\" src=\"https:\/\/anvil-of-time.com\/wp-content\/uploads\/2013\/02\/eventcreatemessage.png\" alt=\"eventcreatemessage\" width=\"700\" height=\"762\" srcset=\"https:\/\/anvil-of-time.com\/wp-content\/uploads\/2013\/02\/eventcreatemessage.png 700w, https:\/\/anvil-of-time.com\/wp-content\/uploads\/2013\/02\/eventcreatemessage-275x300.png 275w, https:\/\/anvil-of-time.com\/wp-content\/uploads\/2013\/02\/eventcreatemessage-137x150.png 137w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><\/p>\n<p>To use this the user must be an administrator on the machine or you have to provide the id\/password combination of one.<\/p>\n<p>Thanks to Kyle Griffis for this tip.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here are \u00a0two ways to write to the Event log in Windows and indicate the source of the message. Reportevent External Function and Eventcreate command line command You can use the command shell to do this too but\u00a0this has the drawback of not being able to assign the source of the event. First you need&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,10],"tags":[70,12,16,69,42,68],"_links":{"self":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/1538"}],"collection":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/comments?post=1538"}],"version-history":[{"count":15,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/1538\/revisions"}],"predecessor-version":[{"id":2065,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/1538\/revisions\/2065"}],"wp:attachment":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/media?parent=1538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/categories?post=1538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/tags?post=1538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}