gpt4 book ai didi

c# - AppDomain.CurrentDomain.UnhandledException 没有被调用

转载 作者:太空狗 更新时间:2023-10-29 21:13:24 25 4
gpt4 key购买 nike

我有一个 WCF 服务,在 Global.asax 中有以下代码:

protected void Application_Start(object sender, EventArgs e)
{
// Make sure that any exceptions that we don't handle at least get logged.
AppDomain.CurrentDomain.UnhandledException += LogUnhandledException;
}

private void LogUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Log.Error.LogException("UnhandledException", e.ExceptionObject as Exception);
}

想法是至少记录所有未处理的异常。

但它似乎从来没有被调用过。我尝试在我的一个服务操作中执行除以零操作,它在遇到异常后就停止了服务。

int zero = 0;
int result = 100 / zero;

LogUnhandledException 方法永远不会被调用。

我在 IIS 和调试器中都试过了。

我怎样才能让这个事件为 WCF 服务工作?

最佳答案

unhandled exception filter for an app domain是允许应用程序在终止之前记录有意义的信息的最后尝试。

This event provides notification of uncaught exceptions. It allows the application to log information about the exception before the system default handler reports the exception to the user and terminates the application.

如果 WCF 允许以这种方式完全不处理服务抛出的异常,则意味着当服务托管在 IIS 中时,整个工作进程将终止,因为单个请求引发了异常——这不是理想的结果。因此,WCF 不会留下未处理的服务抛出的异常 - 在这种情况下不会引发此事件

如果您想记录 WCF 服务抛出的异常,请查看 IErrorHandler interface相反。

关于c# - AppDomain.CurrentDomain.UnhandledException 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16348797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com