使用 Elmah 处理 Web 服务中的异常 [英] Handle exceptions in web services with Elmah

查看:30
本文介绍了使用 Elmah 处理 Web 服务中的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法像我们在 ASP.NET 网站中那样使用 ELMAH 全局处理常规 ASP.NET Web 服务 (asmx) 中的异常?

解决方案

ASP.NET Web 服务从不触发 Application_Error 事件,ELMAH 无法像在 ASP.NET 应用程序中那样全局处理异常.但是我们可以使用 ELMAH 手动"记录异常:

<前><代码>公共 int WebServiceMethod() {尝试 {...}捕获(异常前){Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(new Elmah.Error(ex, HttpContext.Current));}}

Is there a way to globally handle exceptions in regular ASP.NET Web Service (asmx) using ELMAH like we do it in ASP.NET web site ?

解决方案

ASP.NET web services never fire Application_Error event and exceptions cannot be handled globally by ELMAH like in ASP.NET apps. But we can "manually" log exceptions using ELMAH:


public int WebServiceMethod() {
  try {
   ...
  }
  catch (Exception ex) {
    Elmah.ErrorLog.GetDefault(
      HttpContext.Current).Log(new Elmah.Error(ex, HttpContext.Current));
  }
}

这篇关于使用 Elmah 处理 Web 服务中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆