You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
BookingHeChuan/ServiceDeamon/CheckServiceHelper.cs

41 lines
1.1 KiB
C#

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