c# Windows服务偶尔重新启动
我用 C# 编写了一个 Windows 服务,其设计目的是不停止或重新启动。
在构造函数中,有一个日志,上面写着“正在启动应用程序”之类的内容。
所以我让它运行了一个多星期,我可以看到写入日志的构造函数正在执行。这让我相信 Windows 服务正在因未知原因重新启动。没有抛出任何错误!
有什么想法吗?
干杯。
I have written a windows service in c# that's designed not to stop or restart.
In the constructor, there is a log that writes something like "Starting Application".
So i left it running for more than a week and I can see that the constructor that writes the log is being executed. This leads me to believe the windows service is restarting for unknown reason. There are no errors being thrown!!
Any idea?
Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常,您可以通过查看事件查看器来了解服务重新启动的原因。从开始菜单下的管理工具打开它。查看 Windows 日志/应用程序下方。查找带有您的程序名称的任何内容或带有红色感叹号的任何内容。
通常,在编写 Windows 服务时,您希望所有代码都在 TRY/CATCH 块内运行。如果需要,您可以在 catch 块中记录任何错误(但请注意您的日志记录代码不能引发异常!)。你必须“吞掉”异常才能让服务继续运行。
Usually you can see the reason for a service restart by looking in the event viewer. Open it from Administrative Tools under your start menu. Look underneath Windows Logs/Application. Look for anything with your program name or anything that has a Red exclamation mark.
Generally, when writing a windows service, you want ALL of your code to run within a TRY/CATCH block. You can log any error in the catch block if you want (but be careful that your logging code can't throw an exception!). You have to "swallow" the exception in order to let the service keep running.