无法在Windows服务中查看来自OnStop事件的日志

时间:2015-12-14 11:03:34

标签: c# windows-services

我有一个简单的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);
        }
    }
}

0 个答案:

没有答案