简体   繁体   中英

Elmah with log4net doesn't log

I'm new in Elmah's world and I'm trying to see my log (in a web.api project) with it, but it doesn't work. I think I'm missing some configuration :(
Here is the configuration I have:

[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>

Can anyone tell me what I'm missing?

Note: If I use another appender like (FileAppender), it works!

Thanks!

I finally solved my problem after read a lot of foros and posts. I created an ActionFilterAttribute and after that, all my exceptions are logged OK in ELMAH. Here is my code in case anyone will need it.

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()));
        }
    }
}

And of course, you need to register your filter in WebApiConfig.cs

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

Enjoy it! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM