繁体   English   中英

调试和记录Windows服务

[英]Debugging and logging windows service

我正在学习Windows服务的基础知识。 我创建了一个非常简单的。

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);
            }
        }
    }
}

我的服务(AServis)第一次成功启动,但是当我单击重新启动时,它崩溃了。 由于我的服务非常简单,因此应该可以正常使用。 我尝试记录该错误,尝试将其捕获,但找不到任何内容。 我正在尝试附加进程,它调试stop事件,但是在stop调试突然结束并启动进程崩溃之后。 您能帮我什么原因,如何调试和记录错误。

提前致谢

在此处输入图片说明

我看到它被卡在

public OmerService()
{
    InitializeComponent();
}

我可以看到添加System.Diagnostics.Debugger.Launch()的问题; 声明。

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

在这种情况下,我使用的标准技巧是在启动代码中添加对System.Diagnostics.Debugger.Break的调用。 现在,当您正常启动服务(通过服务控制管理器(SCM))时,对Break的调用将导致Windows启动JIT调试器,这将提示您选择要附加到该进程的调试器(例如Visual Studio),这将使您能够正常调试代码。

另请参见: 调试Windows服务的简便方法

暂无
暂无

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

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