异常处理Windows服务中的最佳做法? [英] Exception handling best practice in a windows service?

查看:208
本文介绍了异常处理Windows服务中的最佳做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个完全在后台运行的Windows服务,每天都做一些事情。我的想法是,服务应该非常稳定,所以如果出现问题,它不应该停止,而是再次尝试,当然也记录异常。你能否建议我最好的做法如何使真正稳定的Windows服务?



我已阅读 Scott Hanselman的文章的异常处理最佳实践,他写道,只有少数情况下你应该吞下一个例外。我认为Windows服务是少数几种情况之一,但我很乐意得到一些确认。

解决方案

吞咽一个例外与放弃特定任务而不停止整个过程不同。
在我们的Windows服务中,我们捕获异常,记录其详细信息,然后优雅降级该任务并等待下一个任务。然后,我们可以在服务器仍在运行时使用该日志排除错误。


I am currently writing a windows service that runs entirely in the background and does something every day. My idea is that the service should be very stable so if something goes wrong it should not stop but try it next day again and of course log the exception. Can you suggest me any best practice how to make truly stable windows services?

I have read the article of Scott Hanselman of exception handling best practice where he writes that there are only few cases when you should swallow an exception. I think somehow that windows service is one of the few cases, but I would be happy to get some confirmation on that.

解决方案

'Swallowing' an exception is different to 'abandoning a specific task without stopping the entire process'. In our windows service, we catch exceptions, log their details, then gracefully degrade that task and wait for the next task. We can then use the log to troubleshoot the error while the server is still running.

这篇关于异常处理Windows服务中的最佳做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆