我有一个简单的Windows Service
应用程序,可以作为用户双击启动的服务或应用程序运行。问题是我没有来自OnStop
事件处理程序的日志。 OnStart
处理程序没有问题。为什么我看不到来自OnStop
的任何日志?
public partial class TransmedicomCentralService : ServiceBase
{
TransmedicomCentralWorker serverThread;
public TransmedicomCentralService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
// konfiguracja loggera
GostcompUtils.Logger.Level = GostcompUtils.LoggerLevel.Trace;
GostcompUtils.Logger.FileName = "nowyPlikLogow.txt";
GostcompUtils.Logger.Targets = new List<GostcompUtils.LoggerTarget>() { GostcompUtils.LoggerTarget.File };
base.OnStart(args);
GostcompUtils.Logger.Log("Rozruch serwisu", GostcompUtils.LoggerLevel.Info);
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
serverThread = new TransmedicomCentralWorker();
GostcompUtils.Logger.Log("Serwis uruchomiono", GostcompUtils.LoggerLevel.Info);
}
protected override void OnStop()
{
base.OnStop();
GostcompUtils.Logger.Log("Zatrzymywanie serwisu", GostcompUtils.LoggerLevel.Info);
serverThread.Dispose();
GostcompUtils.Logger.Log("Serwis zatrzymano", GostcompUtils.LoggerLevel.Info);
}
public static void Main(string[] args)
{
TransmedicomCentralService service = new TransmedicomCentralService();
if (Environment.UserInteractive)
{
service.OnStart(args);
Console.WriteLine("Press any key to stop program");
Console.Read();
service.OnStop();
}
else
{
ServiceBase.Run(service);
}
}
}