C# 调试和记录windows服务
我正在学习windows服务的基础知识。我创建了一个非常简单的C# 调试和记录windows服务,c#,visual-studio,windows-services,C#,Visual Studio,Windows Services,我正在学习windows服务的基础知识。我创建了一个非常简单的 using System.ServiceProcess; namespace WindowsServiceBasic { public partial class OmerService : ServiceBase { public OmerService() { InitializeComponent(); } protec
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);
}
}
}
}
使用System.ServiceProcess;
命名空间WindowsServiceBasic
{
公共部分类服务:ServiceBase
{
公共服务()
{
初始化组件();
}
启动时受保护的覆盖无效(字符串[]args)
{
System.Diagnostics.Debugger.Launch();
书面记录(“开始”);
}
受保护的覆盖void OnStop()
{
System.Diagnostics.Debugger.Launch();
书面记录(“停止”);
}
私有无效写日志(字符串硬拷贝)
{
eventLog1.WriteEntry(performanceCounter1.RawValue.ToString());
}
}
}
使用制度;
使用System.IO;
使用System.ServiceProcess;
命名空间WindowsServiceBasic
{
内部静态类程序
{
///
///应用程序的主要入口点。
///
私有静态void Main()
{
AppDomain.CurrentDomain.UnhandledException+=CurrentDomain\u UnhandledException;
ServiceBase[]ServicesToRun;
ServicesToRun=新的ServiceBase[]
{
新服务()
};
ServiceBase.Run(ServicesToRun);
}
私有静态void CurrentDomain_UnhandledException(对象发送方,UnhandledExceptionEventArgs e)
{
如果(e!=null&&e.ExceptionObject!=null)
{
字符串createText=e.ToString();
writealText(@“c:\omerlog.txt”,createText);
}
}
}
}
第一次成功启动我的服务(AServis)时,但当我单击“重新启动”按钮时,它崩溃。因为我的服务很简单,所以应该能正常工作。我尝试记录错误,放置try catch,但找不到任何内容。我试图附加进程,它调试停止事件,但在停止调试后突然完成,启动进程崩溃。你能帮助我什么是原因,我如何调试和记录错误
提前谢谢
在这种情况下,我使用的标准技巧是在启动代码中添加对的调用。现在,当您正常启动服务时(通过服务控制管理器(SCM)),对中断的调用将导致Windows启动JIT调试器,该调试器将提示您选择要附加到进程的调试器(例如Visual Studio),从而使您能够正常调试代码
还可以看到:。我看到它卡在
public OmerService()
{
InitializeComponent();
}
我可以看到添加System.Diagnostics.Debugger.Launch()时出现的问题;声明
public OmerService()
{
System.Diagnostics.Debugger.Launch();
InitializeComponent();
}
我添加了System.Diagnostics.Debugger.Launch();但它在第一次启动时会再次中断,但在停止并重试启动后,它会崩溃,甚至不会中断System.Diagnostics.Debugger.Launch();请你再帮忙好吗。我怀疑performanceCounter1导致了错误。但是我如何调试它呢?你把启动调用放在你服务的OnStart方法中了吗?是的,你可以找到我编辑过的帖子。它在第一次呼叫时进入onstart,但在停止和启动后不进入。