c# - 如何检测 Windows 服务是否已崩溃?
问题描述
我安装了 Windows 服务,它正在运行。
当系统关闭时,我写入事件日志条目,但是代码不会在系统关闭或重新启动时写入事件日志。
protected override void OnShutdown()
{
WriteToEventLog("Services shut down.");
}
private void WriteToEventLog(string logMessage)
{
var mySource = "MySourceLog";
// Create the source and log, if it does not already exist.
if (!EventLog.SourceExists(mySource))
{
EventLog.CreateEventSource(mySource, mySource);
}
// Create an EventLog instance and assign its source.
using (var eventLog = new EventLog())
{
eventLog.Source = mySource;
// Write an entry to the event log.
eventLog.WriteEntry(logMessage, EventLogEntryType.Warning, 1002);
}
}
每当我的 Windows 服务崩溃时,我想将一个事件写入事件日志,并可能发送一封电子邮件作为警报,说明该服务已崩溃。我如何检测到该服务已崩溃并将其写入事件日志?
解决方案
推荐阅读
- android - 通过 SmsManager 发送短信时应用程序崩溃
- sql - 返回计数最高的行(如交易最多的城市,当有多个城市时)
- git - 如何将值传递给 Gitlab CI 作业
- paperjs - 如何在 Paper.js 中划分多个形状,例如在 Illustrator 中使用 Pathfinder
- angular - 即使 tsc 版本足够高,也不会构建带有 bang 运算符的打字稿文件
- java - 为什么在 Google Play 商店上传应用程序时出现 SslErrorHandler?
- php - Laravel 单元测试的 PDO 扩展
- python - 如何使用 django-siteflags 为站点对象添加书签?
- php - 用于计算常见贷款类型(如抵押贷款、汽车贷款、学生贷款或个人贷款)的基本计算公式
- excel - 从根节点检索空子节点