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

how do i access the elmah log when used in web api

我按照此处所述安装了 elmah https://github.com/rdingwall/elmah-contrib-webapi

现在我正在尝试访问日志记录,但我保留了 getter 错误 404 错误。 我使用的 url 正确吗?是否还缺少一些配置?

我的 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 does not work, via http://localhost/MagnusREST/elmah.axd does not work and http://localhost/elmah.axd 访问日志也不起作用。

我做错了什么?

这是 运行 2013 年 visual studio。

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 contrib 的文档和问题。好了第一个问题通过了,下一个是什么;-)

要充分发挥作用,需要比上述更多的东西

添加配置部分

<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.web 服务器需要像这样:

<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 的地方,并将这些内容添加到您自己的配置文件中,这些内容可能看起来略有不同。

我还必须将其添加到我的 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>