using NLog; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.ServiceProcess; using System.Text; using System.Threading.Tasks; namespace ServiceDeamon { public class CheckServiceHelper { private static string ServName = ConfigurationManager.AppSettings["ServiceName"]; private static readonly Logger log = LogManager.GetCurrentClassLogger(); private static ServiceControllerStatus LastStatus; public static void CheckStatus() { var servCtl = new ServiceController(ServName); if (LastStatus != servCtl.Status) { log.Info($"服务 {ServName} 的当前状态:{servCtl.Status}"); } if (servCtl != null && servCtl.Status == ServiceControllerStatus.Stopped) { log.Info($"服务已停止,自动启动:{ServName}"); servCtl.Start(); } LastStatus = servCtl.Status; //Console.WriteLine("CheckStatus"); } } }