将多线程 Windows 服务中的异常写入事件日志

发布于 2024-09-05 09:03:27 字数 142 浏览 2 评论 0原文

我有一个多线程 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人生戏 2024-09-12 09:03:27

首先,您生成的线程应该有一个顶级 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 the AppDomain.UnhandledException event to catch any unhandled exceptions and log them out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文