在web api中使用时如何访问elmah日志

时间:2015-12-05 13:12:42

标签: asp.net-web-api elmah

我按照https://github.com/rdingwall/elmah-contrib-webapi

所述安装了elmah

现在我正在尝试访问日志记录但我保持getter错误404错误。 我使用的是正确的网址吗?还有一些配置缺失吗?

我的webapi运行如下: http://localhost/MagnusREST/api/Customers/054036?frmt=xml

我的application_start看起来像这样:

protected void Application_Start()
{
    GlobalConfiguration.Configuration.Filters.Add(new ElmahHandleErrorApiAttribute()); //added for elmah
    AreaRegistration.RegisterAllAreas();
    UnityConfig.RegisterComponents();  
    GlobalConfiguration.Configure(WebApiConfig.Register);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

我的webapi配置保持不变,看起来像这样:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services


        // Web API routes
        config.MapHttpAttributeRoutes();

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

        config.Formatters.JsonFormatter.MediaTypeMappings.Add(
            new QueryStringMapping("frmt", "json",
                new MediaTypeHeaderValue("application/json")));

        config.Formatters.XmlFormatter.MediaTypeMappings.Add(
            new QueryStringMapping("frmt", "xml",
                new MediaTypeHeaderValue("application/xml")));

        //config.Formatters.Clear();
        JsonMediaTypeFormatter oldformatter = config.Formatters.Where(f => f is JsonMediaTypeFormatter).FirstOrDefault() as JsonMediaTypeFormatter;
        if (oldformatter != null) config.Formatters.Remove(oldformatter);
        config.Formatters.Insert(0,new PartialJsonMediaTypeFormatter() { IgnoreCase = true });
    }
}

通过http://localhost/MagnusREST/api/elmah.axd访问日志无效,http://localhost/MagnusREST/elmah.axd无效,http://localhost/elmah.axd也不起作用。

我做错了什么?

这是在visual studio 2013中运行。

2 个答案:

答案 0 :(得分:1)

web.config中有一个缺失的部分

<handlers>
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <remove name="OPTIONSVerbHandler" />
  <remove name="TRACEVerbHandler" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
  <add name="elmah" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
</handlers>

需要添加elma处理程序。在这之后我可以使用followin url访问elmah日志:

http://localhost/MagnusREST/elmah.axd

你必须阅读elmah和webapi的文档和问题。好的,第一个问题通过,接下来会是什么; - )

要获得完全正常工作需要比上面的更多

添加配置

<configSections>
    <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>
</configSections>

添加您喜欢的elmah配置以发送邮件:

<elmah>
    <security allowRemoteAccess="0" />
    <errorMail from="xxxx" to="xxx" subject="some error" async="true"
           smtpPort="25" smtpServer="xxx" userName="xxx" password="xxx"/>         
</elmah>

system.web和system.webserver必须是这样的:

<system.web>
    <compilation debug="true" targetFramework="4.5.1" />
    <httpRuntime targetFramework="4.5.1" />
    <httpHandlers>
      <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
    </httpHandlers>
    <httpModules>
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
      <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah"/>
    </httpModules>
</system.web>
<system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules>
      <add name="Elmah.ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
      <add name="Elmah.ErrorMail" type="Elmah.ErrorMailModule" preCondition="managedHandler" />
    </modules>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
      <add name="elmah" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
    </handlers>
</system.webServer>

仔细查看elmah被提及的位置,并将这些内容添加到您自己的配置文件中,这些文件看起来可能略有不同。

答案 1 :(得分:0)

我还必须将此添加到我的web.config并且它们有效

<location path="elmah.axd" inheritInChildApplications="false">
<system.web>
  <httpHandlers>
    <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
  </httpHandlers>
  <!-- 
    See http://code.google.com/p/elmah/wiki/SecuringErrorLogPages for 
    more information on using ASP.NET authorization securing ELMAH.

  <authorization>
    <allow roles="admin" />
    <deny users="*" />  
  </authorization>
  -->
</system.web>
<system.webServer>
  <handlers>
    <add name="ELMAH" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />
  </handlers>
</system.webServer>