简体   繁体   English

NodeJS-登录到Windows事件日志?

[英]NodeJS - Logging to Windows Event Log?

I am trying to write a Node app that, once deployed, will log to Windows Event Log. 我正在尝试编写一个Node应用程序,该应用程序一旦部署,将记录到Windows事件日志中。

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. 当我使用命令行运行应用程序时,我浏览了可用的软件包,并能够成功使用节点窗口将日志写入Windows Event Viewer。 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. 但是,当我使用pkg将应用程序转换为.exe文件并尝试运行.exe文件时,它不再登录到Windows事件日志中。

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 (eg node index.js), it logs to Windows Event Viewer. 如果我使用命令行(例如,节点index.js)运行此应用程序,它将记录到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. 但是,当我运行pkg将项目转换为可执行文件并尝试运行可执行文件(作为实例并使用Windows Task Scheduler)时,它不再注销。

I have already checked to ensure that the .exe is running as administrator, so I don't think it is an issue with permissions. 我已经检查过以确保.exe以管理员身份运行,因此我认为这不是权限问题。 Anyone have any thoughts on why the .exe may not be logging out? 有人对为什么.exe可能未注销有任何想法吗? Are there any other NPM packages/libraries that provide the ability to log to Windows Event Log? 是否有其他NPM软件包/库可提供登录Windows事件日志的功能?

Thank you in advance! 先感谢您!

pkg packages js source files and other assets into an EXE, and exposes them within a node process using a virtual file system. pkg将js源文件和其他资产打包到EXE中,并使用虚拟文件系统在节点进程中公开它们。 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. 对于其大多数功能,node-windows模块分发一些自己的可执行文件或使用Windows内置函数。 It exec() s those EXEs on your behalf. 它代表您exec()那些EXE。

  • For the helpers distributed by the module, the EXE must exist on the real filesystem in order to exec() it. 对于由模块分发的帮助程序,EXE必须存在于真实的文件系统上才能exec() It is highly likely that pkg breaks this; pkg很有可能打破了这一点; you'd need to distribute those EXEs and place them on disk yourself. 您需要分发这些EXE并将它们自己放置在磁盘上。
  • 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. 事件日志记录使用内置的eventcreate ,它应该可以工作,但是pkg可能child_process虚拟文件系统做一些奇怪的事情,从而破坏了child_process工作方式。

At any rate, I strongly recommend against using node-windows for logging because it exec() sa new child process for every log call. 无论如何,我强烈建议不要使用节点窗口进行日志记录,因为exec()是每个日志调用的新子进程。 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. 这是非常低效的在Windows上:它必须产生一个壳(CMD.EXE),然后运行助手(eventcreate.exe),而在Windows进程创建是缓慢的。 Painfully slow. 痛苦的缓慢。

程序启动图

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. 而是使用直接调用ReportEvent API的本机模块。 windows-eventlog fits this bill. windows-eventlog符合此要求。

You'll also want to consider pkg's notes regarding native modules . 您还需要考虑pkg关于本机模块的注释

Native addons ( .node files) use is supported, but packaging .node files inside the executable is not resolved yet. 支持使用本机插件( .node文件),但尚未解析将.node文件打包在可执行文件中。 You have to deploy native addons used by your project to the same directory as the executable. 您必须将项目使用的本机加载项部署到与可执行文件相同的目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM