3

I am trying to write a Node app that, once deployed, will log to Windows Event Log.

I looked through available packages and was able to successfully use node-windows to write logs to the Windows Event Viewer when I ran the app using the command line. However, when I used pkg to turn the app into an .exe file and tried to run the .exe file, it was no longer logging to Windows Event Log.

As an example, I tried writing a basic app as follows:

const EventLogger = require('node-windows').EventLogger;

const log = new EventLogger('TestApp');

log.info('Test test test!', 1000)

If I run this app using the command line (e.g. node index.js), it logs to Windows Event Viewer. However, when I run pkg to convert the project into an executable file and try to run the executable file (both as an instance and using Windows Task Scheduler), it no longer logs out.

I have already checked to ensure that the .exe is running as administrator, so I don't think it is an issue with permissions. Anyone have any thoughts on why the .exe may not be logging out? Are there any other NPM packages/libraries that provide the ability to log to Windows Event Log?

Thank you in advance!

uabdul
  • 31
  • 1
  • 2

1 Answers1

2

pkg packages js source files and other assets into an EXE, and exposes them within a node process using a virtual file system. This works fine for most things, but breaks if any of those assets need to be used outside of your process.

For most of its functionality, the node-windows module distributes a few executables of its own or uses Windows builtins. It exec()s those EXEs on your behalf.

  • For the helpers distributed by the module, the EXE must exist on the real filesystem in order to exec() it. It is highly likely that pkg breaks this; you'd need to distribute those EXEs and place them on disk yourself.
  • Event logging uses the builtin eventcreate, which should work, but maybe pkg is doing something weird with the virtual filesystem that breaks how child_process works.

At any rate, I strongly recommend against using node-windows for logging because it exec()s a new child process for every log call. This is highly inefficient on Windows: it has to spawn a shell (cmd.exe) and then run the helper (eventcreate.exe), and process creation on Windows is slow. Painfully slow.

Program launch graph

If you start logging enough things, you'll soon bring your entire system to its knees with process bookkeeping.

Instead, use a native module that calls the ReportEvent API directly. windows-eventlog fits this bill.

You'll also want to consider pkg's notes regarding native modules.

Native addons (.node files) use is supported, but packaging .node files inside the executable is not resolved yet. You have to deploy native addons used by your project to the same directory as the executable.

josh3736
  • 139,160
  • 33
  • 216
  • 263