ASP.NET Web服务忽略Application_Error和UnhandledException事件
问题描述:
我的web服务中有一个被忽略的异常事件的小问题。ASP.NET Web服务忽略Application_Error和UnhandledException事件
这是我的Global.asax.cs文件:
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
namespace AcmeDemoApp
{
public class Global : System.Web.HttpApplication
{
private static Logger logger = LogManager.GetCurrentClassLogger();
protected void Application_Start(object sender, EventArgs e)
{
logger.Debug("Application_Start");
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}
protected void Session_Start(object sender, EventArgs e)
{
logger.Debug("Session_Start");
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
logger.Debug("Application_BeginRequest");
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
logger.Debug("Application_AuthenticateRequest");
}
protected void Application_Error(object sender, EventArgs e)
{
logger.Debug("Application_Error");
}
protected void Session_End(object sender, EventArgs e)
{
logger.Debug("Session_End");
}
protected void Application_End(object sender, EventArgs e)
{
logger.Debug("Application_End");
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
logger.Debug("CurrentDomain_UnhandledException");
}
}
}
后,我执行请求。我从Web服务中得到这个错误。
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<soap:Fault>
<soap:Code>
<soap:Value>soap:Receiver</soap:Value>
</soap:Code>
<soap:Reason>
<soap:Text xml:lang="en">System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.ArgumentException: Error
--- End of inner exception stack trace ---</soap:Text>
</soap:Reason>
<soap:Detail />
</soap:Fault>
</soap:Body>
</soap:Envelope>
而在NLOG的日志文件,我只看到:
[2016-05-01 02:11:25.3629] [Info] Application_BeginRequest
[2016-05-01 02:11:27.3861] [Info] Application_AuthenticateRequest
为什么UnhandledException忽略? 是否还有其他事件或方法我必须用来获取所有soap
错误?
或者你能给我一个提示如何解决这个问题?
先谢谢您的任何想法!
答
使用UnhandledExceptionHandler。
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
尝试处理Application.ThreadException – VladL
No @VladL我没有'Application.ThreadException'。 'Application'存在,但没有'ThreadException'事件/属性。 – Patrick