簡體   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