如何记录 Windows 服务因系统关闭而终止?

发布于 2024-08-23 08:04:21 字数 406 浏览 10 评论 0原文

我有一个服务的诊断版本,它在 OnStart() 和 OnStop() 方法中记录尽可能多的信息。

我无法捕获的一个事件是计算机物理重新启动时。我的日志记录功能通常将其输出记录到数据库中的表中,但当该表不可用时,它会发布到事件日志。

重新启动时,我的服务既不记录到表也不记录到事件日志。

对我来说,我无法发布到表中是有道理的,因为 SQL Server 正在关闭过程中,但似乎由于计时问题,EventLog 也可能在服务默认为之前关闭写在那里。

在关闭的情况下,MSSQLSERVER 在事件日志中报告一条信息消息:

由于系统关闭,SQL Server 正在终止。这只是一条信息性消息。无需用户执行任何操作。

有没有办法为我的服务做类似的事情?

I have a diagnostic version of a service that logs as much as possible in the OnStart() and OnStop() methods.

One event that I am unable to capture is when the computer is physically restarted. My logging function usually records its output to a table in a database, but when that is not available it posts to the EventLog.

On a reboot my service neither logs to the table nor to the EventLog.

It makes sense to me that I would not be able to post to the table, since SQL Server is in the process of shutting down, but it also seems that due to a timing issue the EventLog may also be shutting before the service can default to write there.

In the case of a shutdown, MSSQLSERVER reports an information message in the EventLog:

SQL Server is terminating because of a system shutdown. This is an informational message only. No user action is required.

Is there a way to do something similar for my service?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

放血 2024-08-30 08:04:22

您可以指定 Windows 服务的依赖关系以使其需要其他服务。如果您指定对 EventLog 服务的依赖关系,则 Windows 将等到您的服务关闭后再关闭事件日志。

http://kb2.adobe.com/cps/400/kb400960.html 描述如何通过修改几个注册表项来实现此目的。

导航至
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services
并找到您需要的服务
设置依赖关系。打开
右侧的“DependOnService”键
边。如果所选服务不
有一个“DependOnService”键,然后
右键单击并创建一个
选择新建>多字符串值。在
值字段,输入名称
当前服务的所有服务
将取决于。每个服务名称必须
正确输入并单独输入
线。

You can specify the dependencies of your Windows Service to have it require another service. If you specify a dependency on the EventLog service, then Windows will wait until your service is shut down before shutting down the Event Log.

http://kb2.adobe.com/cps/400/kb400960.html describes how to do it by modifying a few registry keys.

Navigate to
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services
and locate the service that you need
to set a dependency for. Open the
'DependOnService' key on the right
side. If the selected service does not
have a 'DependOnService' key, then
create one by right-clicking and
selecting New > Multi-String Value. In
the value field, enter the names of
all services that the current service
will depend on. Each service name must
be entered properly and on a separate
line.

何处潇湘 2024-08-30 08:04:22

您可以在服务中重写 OnShutdown 方法。当机器关闭时它将被调用。从该方法写入事件日志,然后调用 base.OnShutdown()。

There is an OnShutdown method your can override in your service. It will be called when the machine is shutting down. Write to the event log from that method, then call base.OnShutdown().

云雾 2024-08-30 08:04:22

捕获 SystemEvents.SessionEnding

在 OnStart 上 可以捕获它并

处理

Microsoft.Win32.SystemEvents.SessionEnding += new Microsoft.Win32.SessionEndingEventHandler(this.zomgRebootinglol);

Capture SystemEvents.SessionEnding

On the OnStart you could capture it and handle

like

Microsoft.Win32.SystemEvents.SessionEnding += new Microsoft.Win32.SessionEndingEventHandler(this.zomgRebootinglol);
嘦怹 2024-08-30 08:04:22

您可以使用 SystemEvents-Class

另请查看事件
我认为您正在寻找的事件是: SystemEvents::PowerModeChanged Event 和/或 SystemEvents::SessionEnded

You can capture windows events by using the SystemEvents-Class.

Also take a look at the events.
I think the Event you are looking for is: SystemEvents::PowerModeChanged Event and/or SystemEvents::SessionEnded

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文