将多线程 Windows 服务中的异常写入事件日志
我有一个多线程 Windows 服务,它将不可预测地每 24 小时左右停止运行一次。我正在写入事件日志,一切都很好,但是每当服务崩溃时,事件日志中就没有消息(即使服务停止了,尽管 AutoLog=true)。有没有办法将未捕获的异常直接写入日志,即使它们不在原始线程中?
I have a multithreaded windows service that will unpredictably stop running once every 24 hours or so. I am writing to the event log and that's going through just fine, but whenever the service crashes there are no messages in the event log (even that the service stopped, despite having AutoLog=true). Is there a way to have uncaught exceptions written straight to the log, even if they aren't in the original thread?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您生成的线程应该有一个顶级
try { } ... catch { }
块,用于捕获(并且,如果合适,吞咽或重新抛出)抛出的异常。除此之外,您还可以使用AppDomain.UnhandledException
事件来捕获任何未处理的异常并将其注销。First of all, threads that you spawn should have a top level
try { } ... catch { }
block that catches (and, if appropriate, either swallows or rethrows) exceptions thrown on them. Apart from that, you can use theAppDomain.UnhandledException
event to catch any unhandled exceptions and log them out.