【问题标题】:how do i access the elmah log when used in web api在 web api 中使用时如何访问 elmah 日志
【发布时间】:2016-03-10 09:17:18
【问题描述】:

我按照这里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 中运行的。

【问题讨论】:

  • 感谢您的投票,但是如果您围绕这个主题进行谷歌搜索,则信息完全分散且不完整。所以我认为这是一个可行的问题。我试图在下面的答案中完整。

标签: asp.net-web-api elmah


【解决方案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 处理程序。之后,我可以使用以下网址访问 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.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 的位置,并将这些内容添加到您自己的配置文件中,看起来可能略有不同。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2014-07-23
      • 2020-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-24
      • 2018-12-19
      • 1970-01-01
      • 2010-12-01
      相关资源
      最近更新 更多