|
| 1 | +# node-eventlog |
| 2 | +_An event log utility for Windows 10 & Server 2012/2016 that actually works_ |
| 3 | + |
| 4 | +`node-eventlog` is a lightweight C++ based module for Node.js, exclusive for the Windows operating systems, that provides functionality for writing entries to the Event Logs. |
| 5 | + |
| 6 | +I created this module after finding a lack of existing supported packages that provided functionality for writing to the Windows Event Logs. |
| 7 | + |
| 8 | +### Compatibility |
| 9 | + |
| 10 | +This module is written using NAPI, for compatibility with future versions of Node. It’s been tested with 64-bit Node 12.8 on 64-bit Windows 10 & Server 2012/2016- although, it should work with other installments of Node/Windows. |
| 11 | + |
| 12 | +### Installing |
| 13 | + |
| 14 | +I made installing this package really simple utilizing a really cool module called [node-pre-gyp](https://github.com/mapbox/node-pre-gyp). I’m able to host the pre-compiled binaries, here on GitHub, as releases. |
| 15 | + |
| 16 | +To install, simply use the npm package: |
| 17 | +```batch |
| 18 | +npm i node-eventlog |
| 19 | +``` |
| 20 | + |
| 21 | +However, if there is an issue installing the pre-compiled binaries (such as being behind a corporate firewall), it will fallback to compiling from the source, which means you need to have [windows-build-tools](https://www.npmjs.com/package/windows-build-tools) or Visual Studio & Python installed. |
| 22 | + |
| 23 | +### How to use the module |
| 24 | + |
| 25 | +Something I love about TypeScript is that my definitions file tells you everything you really need to know: |
| 26 | +```typescript |
| 27 | +export type Severity = "info" | "warn" | "error"; |
| 28 | + |
| 29 | +export declare class EventLog { |
| 30 | + public readonly source: string; |
| 31 | + |
| 32 | + constructor(source: string); |
| 33 | + |
| 34 | + log(message: string, severity?: Severity, code?: number): Promise<boolean>; |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +Simply import the class, and create a new instance. The constructor requires a `source`, which is the name of the application that will be displayed in the Event Log entry. |
| 39 | + |
| 40 | +The `.log()` method will write a new entry to the _Application_ logs. You can optionally provide the type of log entry to write (_info [default], warn, error_), as well as an event code (_default is 1000_). |
| 41 | + |
| 42 | +> _Note_: the `.log()` method is **_Asynchronous_** |
| 43 | +
|
| 44 | +```javascript |
| 45 | +const { EventLog } = require(‘node-eventlog’); |
| 46 | + |
| 47 | +const AppName = ‘MyTestApp’; |
| 48 | + |
| 49 | +const Test = async () => { |
| 50 | + const logger = new EventLog(AppName); |
| 51 | + console.log(await logger.log(‘Test Message’, ‘info’, 9999)); |
| 52 | +}; |
| 53 | + |
| 54 | +Test(); |
| 55 | +``` |
| 56 | + |
| 57 | +### Suggestions? |
| 58 | + |
| 59 | +There are several other features that could potentially be added such as writing to other sections of the Event Logs or being able to read previous entries. Open a new issue if there’s something you’d like to see added! |
0 commit comments