Welcome, guest | Sign In | My Account | Store | Cart

Services subclassing ServiceFramework report to eventlog events source "PythonService". Here is another way of changing the source name

Python, 43 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""
From "Python Programming on Win32 Chapter 18 - Windows NT Services".

The Event Log facilities of servicemanager use the name of the executable as the application name. 
This means that by default, the application name will be PythonService. 
The only way this can be changed is to take a copy of PythonService.exe 
and rename it to something of your liking (it's only around 20 KB!). 
Alternatively, you may wish to use the Event Log natively, as described later in this chapter.

"""

# Here is another way of changing the source name:

class PyMyService(win32serviceutil.ServiceFramework):
    _svc_name_ = "PyMyService"
    _svc_display_name_ = "Python Service With The Right Name"
    
    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)

    def SvcDoRun(self):
        import servicemanager
        # Add these two lines and the source will be "PyMyService"
        win32evtlogutil.AddSourceToRegistry(self._svc_name_, servicemanager.__file__)
        servicemanager.Initialize(self._svc_name_, servicemanager.__file__)
        #
        servicemanager.LogMsg(
               servicemanager.EVENTLOG_INFORMATION_TYPE,
               servicemanager.PYS_SERVICE_STARTED,
               (self._svc_name_, ""))
	               
        servicemanager.LogInfoMsg("Info")  # Event is 255
        servicemanager.LogErrorMsg("Error")  # Event is 255
        servicemanager.LogWarningMsg("Warn")  # Event is 255
        # or
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
               0xF000, ("Info",))   	# Event is 61440
        servicemanager.LogMsg(servicemanager.EVENTLOG_ERROR_TYPE, 
               0xF001, ("Error",))   	# Event is 61441
        servicemanager.LogMsg(servicemanager.EVENTLOG_WARNING_TYPE, 
               0xF002, ("Warn",))   	# Event is 61442

	# events up to 0xF008 are avaliable

This modification does not create a new message DLL and new Event ID's. Rather we are using events which are already in the ServiceFramework.pyd. This violates a few rulles of eventlog reporting (espesially the one which requires separate ID's for distinct events), but is sufficient for debug and internal purposes.

If you are writing an service which is supposed to be monitored via eventlog "harversting" tools you have to write custom resource DLL (MS! Why couldn't it be a simple text file?)

Created by Lev Elbert on Sat, 10 Jul 2004 (PSF)
Python recipes (4591)
Lev Elbert's recipes (1)

Required Modules

Other Information and Tasks