Is it possible to log who started or stopped a windows service?

前端 未结 5 1866
青春惊慌失措
青春惊慌失措 2020-12-16 12:21

I have some windows services written in C#. When somebody stops or starts the service, I would like to be able to determine who it was and log that information.

I t

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-16 13:05

    There probably isn't a way. Any of the normal .NET ways that you get at the environment's user are going to return the user whose credentials the service runs with (which will typically be SYSTEM, LOCAL SERVICE, NETWORK SERVICE, etc).

    How I'd probably do it is poll the system to see if a user is logged in, and assume that user did it. Of course, this discounts services that are shut down by the system for some reason (presumably your service would not be), and can only help you narrow it down if more than one user is logged in at one time (but then, you could always log both of them).

提交回复
热议问题