如何将一般异常记录到事件日志中?
我有一个 Windows 服务,我需要一个顶级的 try-catch 来捕获任何其他未处理(或冒泡)的异常,将其记录到事件日志中,然后吞掉它,以便服务继续运行。但是,我找不到将异常作为参数的 System.Diagnostics.EventLog.WriteEntry
的任何重载 - 是否没有办法只向事件日志提供异常并让它解析出消息本身?
I have a windows service, in which I want a top level try-catch
that catches any otherwise unhandled (or bubbled) exception, logs it to the Event Log and then swallows it so the service keeps running. However, I can't find any overload to System.Diagnostics.EventLog.WriteEntry
that takes an exception as a parameter - is there no way to just give the event log the exception and let it parse out the message on its own?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,没有标准方法可以将异常传递到内置于 .NET 框架的事件日志。
要以最少的开发工作将异常写入事件日志,您需要编写如下内容:
但通常您会尝试对异常进行一些格式化。
Unfortunately there is no standard way of just passing the Exception to the Eventlog, built in to the .NET framework.
To have an exception written to the EventLog with the smallest development effort, you would need to write something like:
But normally you would try to do some formatting of your exception.