与ELMAH Web服务处理异常
-
24-09-2019 - |
题
有没有办法使用ELMAH来在全球范围内定期ASP.NET Web服务(ASMX)处理异常就像我们在ASP.NET网站做呢?
解决方案
ASP.NET Web服务从来没有火Application_Error
事件和异常不能在全球范围内通过ELMAH在ASP.NET应用程序的处理等。但是,我们可以在“手动”登录使用ELMAH异常:
public int WebServiceMethod() {
try {
...
}
catch (Exception ex) {
Elmah.ErrorLog.GetDefault(
HttpContext.Current).Log(new Elmah.Error(ex, HttpContext.Current));
}
}
其他提示
您可以使用的SoapExtension做到这一点:
using System;
using System.Web.Services.Protocols;
namespace MyNamespace
{
class ELMAHExtension : SoapExtension
{
public override object GetInitializer(Type serviceType)
{ return null; }
public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
{ return null; }
public override void Initialize(object initializer)
{ }
public override void ProcessMessage(SoapMessage message)
{
if (message.Stage == SoapMessageStage.AfterSerialize &&
message.Exception != null)
{
// Log exception here
}
}
}
}
您与下面的行中的web.config寄存器这样的:
<system.web>
<webServices>
<soapExtensionTypes>
<add type="MyNamespace.ELMAHExtension, MyDLL" priority="1" group="1" />
</soapExtensionTypes>
</webServices>
</system.web>
这会给你访问的HttpContext和的SOAPMessage对象应该给你所有你需要了解正在叫什么细节。我想你找回这个阶段的异常将永远是一个的SoapException和你有兴趣在该位可能是内部异常。
可以使用此代码
try{
// your code in here
}
catch (Exception ert)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(ert);
}
不隶属于 StackOverflow