Writing to Windows Event Log using win32evtlog from pywin32 library

Rights

I have a simple python script that will be running on a windows server, I'd like to log specific events throughout the script to the windows event log. Does anyone have a simple and precise example of writing to the windows event log so I can view the event from the event viewer. I've read through the docs for the pywin32 library and I can't find any clear examples. I've tried building an event using:

win32evtlogutil.ReportEvent(ApplicationName, EventID, EventCategory,
                EventType, Inserts, Data, SID)

I've had no success, could someone explain the ReportEvent a bit more in depth?

CristiFati

A simple example:

>>> import sys
>>> import win32evtlogutil
>>> import win32evtlog
>>> import time
>>>
>>> "Python {0:s} on {1:s}".format(sys.version, sys.platform)
'Python 3.5.4 (v3.5.4:3f56838, Aug  8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32'
>>>
>>> DUMMY_EVT_APP_NAME = "Dummy Application"
>>> DUMMY_EVT_ID = 7040  # Got this from another event
>>> DUMMY_EVT_CATEG = 9876
>>> DUMMY_EVT_STRS = ["Dummy event string {0:d}".format(item) for item in range(5)]
>>> DUMMY_EVT_DATA = b"Dummy event data"
>>>
>>> "Current time: {0:s}".format(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
 'Current time: 2018-07-18 20:03:08'
>>>
>>> win32evtlogutil.ReportEvent(
...     DUMMY_EVT_APP_NAME, DUMMY_EVT_ID, eventCategory=DUMMY_EVT_CATEG,
...     eventType=win32evtlog.EVENTLOG_WARNING_TYPE, strings=DUMMY_EVT_STRS,
...     data=DUMMY_EVT_DATA)
>>>

Output:

Event Viewer

You can see the correspondence between the values that I input from code, and the event fields in the (above) image of the Event Viewer (mmc) window.

win32evtlogutil.ReportEvent is part of [GitHub]: mhammond/pywin32 - Python for Windows (pywin32) Extensions, which is a Python wrapper over WINAPIs.

Everything you need to know is explained at [MS.Docs]: ReportEventW function, which is the WINAPI used to accomplish this task. Make sure to read it carefully (and some other URLs that it references) in order to get more familiar about the arguments, what their values could be, and other info.

Make sure not to abuse (tests included), or you might end up getting the event log polluted with lots of garbage data.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Reading windows event log in Python using pywin32 (win32evtlog module)

From Dev

Reading windows event log using win32evtlog module

From Dev

Using ImageGrab with bbox from pywin32's GetWindowRect

From Dev

Keyboard event not sent to window with pywin32

From Dev

Pywin32 Windows service reading directory

From Dev

Get an event object from win32evtlog.EvtQuery results

From Dev

Getting this error using pywin32 module?

From Dev

Writing events to Windows Event Log using Coldfusion

From Dev

How to exit cleanly from flask and waitress running as a windows pywin32 service

From Dev

Python PIP cannot find pywin32 (on windows)

From Java

How to install pywin32 module in windows 7

From Dev

Converting Python win32evtlog objects to xml

From Dev

GetGUIThreadInfo() with pywin32

From Dev

Portable Python com server using pywin32

From Dev

How to mute/unmute sound using pywin32?

From Dev

How to know if a window is maximized using pywin32?

From Dev

How to append an "checkbox" into dialog by using pywin32

From Dev

Pywin32 not saving attachments from Outlook (Restrict.Attachements)

From Dev

How to extract text from shape in visio with pywin32?

From Dev

Get Outlook Group Calendars using Pywin32 (win32com.client)

From Dev

Not able to use the win32gui module from pywin32 in Pycharm

From Dev

Error with accessing one fuction in pywin32 or win32com from python 3.3.5

From Dev

Writing to event log by using data from config file

From Dev

Trying to use win32ui with pywin32 gives: A dynamic link library (DLL) initialization routine failed

From Dev

Sorting with xlwings (pywin32)

From Dev

PyWin32 and Python 3.8.0

From Java

Install pywin32 with pip in Windows 7 does not work in python 3.4.2

From Dev

python3 - Changing values of file details on Windows (pywin32)

From Dev

Having problems with accessing files on a mapped network drive, using pywin32 client

Related Related

  1. 1

    Reading windows event log in Python using pywin32 (win32evtlog module)

  2. 2

    Reading windows event log using win32evtlog module

  3. 3

    Using ImageGrab with bbox from pywin32's GetWindowRect

  4. 4

    Keyboard event not sent to window with pywin32

  5. 5

    Pywin32 Windows service reading directory

  6. 6

    Get an event object from win32evtlog.EvtQuery results

  7. 7

    Getting this error using pywin32 module?

  8. 8

    Writing events to Windows Event Log using Coldfusion

  9. 9

    How to exit cleanly from flask and waitress running as a windows pywin32 service

  10. 10

    Python PIP cannot find pywin32 (on windows)

  11. 11

    How to install pywin32 module in windows 7

  12. 12

    Converting Python win32evtlog objects to xml

  13. 13

    GetGUIThreadInfo() with pywin32

  14. 14

    Portable Python com server using pywin32

  15. 15

    How to mute/unmute sound using pywin32?

  16. 16

    How to know if a window is maximized using pywin32?

  17. 17

    How to append an "checkbox" into dialog by using pywin32

  18. 18

    Pywin32 not saving attachments from Outlook (Restrict.Attachements)

  19. 19

    How to extract text from shape in visio with pywin32?

  20. 20

    Get Outlook Group Calendars using Pywin32 (win32com.client)

  21. 21

    Not able to use the win32gui module from pywin32 in Pycharm

  22. 22

    Error with accessing one fuction in pywin32 or win32com from python 3.3.5

  23. 23

    Writing to event log by using data from config file

  24. 24

    Trying to use win32ui with pywin32 gives: A dynamic link library (DLL) initialization routine failed

  25. 25

    Sorting with xlwings (pywin32)

  26. 26

    PyWin32 and Python 3.8.0

  27. 27

    Install pywin32 with pip in Windows 7 does not work in python 3.4.2

  28. 28

    python3 - Changing values of file details on Windows (pywin32)

  29. 29

    Having problems with accessing files on a mapped network drive, using pywin32 client

HotTag

Archive