简体   繁体   English

调试和记录Windows服务

[英]Debugging and logging windows service

I am learning basics of windows service. 我正在学习Windows服务的基础知识。 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. 我的服务(AServis)第一次成功启动,但是当我单击重新启动时,它崩溃了。 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. 我正在尝试附加进程,它调试stop事件,但是在stop调试突然结束并启动进程崩溃之后。 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(); 我可以看到添加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. 在这种情况下,我使用的标准技巧是在启动代码中添加对System.Diagnostics.Debugger.Break的调用。 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. 现在,当您正常启动服务(通过服务控制管理器(SCM))时,对Break的调用将导致Windows启动JIT调试器,这将提示您选择要附加到该进程的调试器(例如Visual Studio),这将使您能够正常调试代码。

Also see this: Easier way to debug a Windows service . 另请参见: 调试Windows服务的简便方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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