【问题标题】:Elmah with log4net doesn't log带有 log4net 的 Elmah 不记录
【发布时间】:2016-12-04 12:11:51
【问题描述】:

我是 Elmah 世界的新手,我正在尝试使用它查看我的日志(在 web.api 项目中),但它不起作用。我想我缺少一些配置:(
这是我的配置:

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

Web.config

<configSections>
  <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  <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>

<system.web>    
  <httpModules>
    <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>    
  </httpModules>
  <httpHandlers>
    <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
  </httpHandlers>  
</system.web>

<elmah>
  <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~\App_Data\" />
  <security allowRemoteAccess="1" />
</elmah>

<log4net>
  <logger name="PROJECT">
    <level value="ALL" />
    <appender-ref ref="elmahappender" />
  </logger>
  <!--ELMAH Appender--> 
  <appender name="elmahappender" type="elmahappender_log4net.ELMAHAppender, elmahappender_log4net">
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [thread] %-5level %logger - %message%newline" />
    </layout>
    <UseNullContext>False</UseNullContext>
  </appender>
</log4net>

<system.webServer>
  <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>
  <modules runAllManagedModulesForAllRequests="true">
    <remove name="FormsAuthentication" />
    <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
  </modules>    
  <validation validateIntegratedModeConfiguration="false" />
</system.webServer>

谁能告诉我我错过了什么?

注意:如果我使用另一个像 (FileAppender) 这样的附加程序,它可以工作!

谢谢!

【问题讨论】:

  • 您的第一步应该是启用 log4net 调试日志并查找任何错误。这个blog post from 10 years ago 会有所帮助。
  • 您好 Stuartd,感谢您的回答,但我的问题不是这样。当我使用 FileAppender 作为附加程序时,Log4net 工作正常,但是当我使用 ELMAH 时它不起作用,所以我认为我缺少一些配置。如果您有任何其他想法,欢迎提出。
  • Log4net 将使用正确配置的附加程序,并忽略那些未正确配置的附加程序 - 但它也会输出 what 配置的详细信息对调试日志记录无效。

标签: c# asp.net-web-api2 log4net elmah


【解决方案1】:

在阅读了很多论坛和帖子后,我终于解决了我的问题。我创建了一个 ActionFilterAttribute,之后,我的所有异常都在 ELMAH 中记录下来。这是我的代码,以防任何人需要它。

public class ElmahLogger : ActionFilterAttribute
{
   public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
      {
         if ((actionExecutedContext.Response != null) && !actionExecutedContext.Response.IsSuccessStatusCode)
         {
            try
            {
                var messages = (System.Web.Http.HttpError)((System.Net.Http.ObjectContent<System.Web.Http.HttpError>)actionExecutedContext.Response.Content).Value;
                StringBuilder stringBuilder = new StringBuilder();
                foreach (var keyValuePair in messages)
                    stringBuilder.AppendLine(keyValuePair.Value.ToString());

                Elmah.ErrorSignal.FromCurrentContext().Raise(new HttpException((int)actionExecutedContext.Response.StatusCode, stringBuilder.ToString()));
            }
            catch (Exception ex)
                Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception("Error in ElmahLoggerFilter - OnActionExecuted: " + ex.ToString()));
        }
    }
}

当然,您需要在 WebApiConfig.cs 中注册您的过滤器

public static class WebApiConfig
{
   public static void Register(HttpConfiguration config)
    {
        config.Filters.Add(new ElmahLogger());
    }
}

尽情享受吧! :)

【讨论】:

  • 您不应该自己编写该代码。有一个contrib 项目供 ELMAH 处理 Web API。我写了一篇关于如何使用elmah.io 安装和配置它的帖子(docs.elmah.io/logging-to-elmah-io-from-web-api),但过程与 XML 记录器相同。只是不要安装 elmah.io 包,而只安装 elmah.corelibrary 并使用单线配置它:)
  • 感谢@ThomasArdal 的帖子!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-28
  • 1970-01-01
  • 1970-01-01
  • 2012-05-25
  • 1970-01-01
  • 2011-06-04
  • 1970-01-01
相关资源
最近更新 更多