Elmah在MVC中工作但在Web API

时间:2018-04-07 19:00:49

标签: asp.net-web-api elmah

我有ELMAH工作的Web应用程序端控制器,但我无法在Web API端记录异常。

我的设置:

WebApiConfig.cs:

public static void Register(HttpConfiguration config)
{
    config.Services.Add(typeof(IExceptionLogger), new ElmahExceptionLogger());

    // Web API routes
    config.MapHttpAttributeRoutes();

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );
}

Web配置:

<sectionGroup name="elmah">
    <section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" />
    <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
    <section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
    <section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" />
</sectionGroup>
....
<appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="elmah.mvc.disableHandler" value="false" />
    <add key="elmah.mvc.disableHandleErrorFilter" value="false" />
    <add key="elmah.mvc.requiresAuthentication" value="false" />
    <add key="elmah.mvc.IgnoreDefaultRoute" value="false" />
    <add key="elmah.mvc.allowedRoles" value="*" />
    <add key="elmah.mvc.allowedUsers" value="*" />
    <add key="elmah.mvc.route" value="elmah" />
    <add key="elmah.mvc.UserAuthCaseSensitive" value="true" />
</appSettings>
....
<modules>
  <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
  <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />
  <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" />
</modules>
....
<httpModules>
    <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
    <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
    <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
</httpModules>
....
<elmah>
    <errorMail key="elmahMail" from="noreply@mydomain.com" to="me@mydomain.com" cc="" subject="DEV EMP Exception" async="true" smtpPort="25" smtpServer="smtp.mydomain.com" userName="" password="" />
</elmah>

我试过这个:

GlobalConfiguration.Configuration.Filters.Add(new ElmahHandleErrorApiAttribute());

通常安装Elmah.Mvc包和Elmah.Contrib.WebApi包并添加:

config.Services.Add(typeof(IExceptionLogger), new ElmahExceptionLogger());

行启用Elmah以获取Web API,但它已不再有效。

1 个答案:

答案 0 :(得分:0)

答案是它一直在工作。 Elmah曾经在404上被触发。现在不再是这种情况了。我正在通过尝试一条不存在的路线进行测试。