简体   繁体   中英

Debugging and logging windows service

I am learning basics of windows service. I have created a very simple one.

using System.ServiceProcess;

namespace WindowsServiceBasic
{
    public partial class OmerService : ServiceBase
    {
        public OmerService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            System.Diagnostics.Debugger.Launch();
            WriteLog("START");
        }

        protected override void OnStop()
        {
            System.Diagnostics.Debugger.Launch();
            WriteLog("STOP");
        }

        private void WriteLog(string durum)
        {
            eventLog1.WriteEntry(performanceCounter1.RawValue.ToString());
        }
    }
}

using System;
using System.IO;
using System.ServiceProcess;

namespace WindowsServiceBasic
{
    internal static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        private static void Main()
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[]
            {
                new OmerService()
            };
            ServiceBase.Run(ServicesToRun);
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            if (e != null && e.ExceptionObject != null)
            {
                string createText = e.ToString();
                File.WriteAllText(@"c:\omerlog.txt", createText);
            }
        }
    }
}

The first time my service (AServis) starts successfully but when I click the restart it crashes. Since my service is very simple It should have been worked properly. I try to log the error, put try catch but I could not find anything. I am trying to attach process, it debugs stop event but after stop debug suddenly finishes and start process crashes. Could you please help me what is the reason and how can I debug and log error.

Thanks in advance

在此处输入图片说明

I saw that it was stuck in

public OmerService()
{
    InitializeComponent();
}

I could see the issue adding System.Diagnostics.Debugger.Launch(); statement.

public OmerService()
{
    System.Diagnostics.Debugger.Launch();
    InitializeComponent();
}

The standard trick I use in this situation is to add a call to System.Diagnostics.Debugger.Break in my start up code. Now, when you start the service as normal (through the Service Control Manager (SCM)), the call to the Break will cause Windows to launch the JIT debugger, which should prompt you to choose the debugger you wish to attach to the process (eg, Visual Studio), which will then enable you to debug your code as normal.

Also see this: Easier way to debug a Windows service .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM