3

I have a Web Api application that has Elmah configured and working, logs are created normally.

In order to hide/remove sensitive data from the logs I tried this, but It doesn't work for my Web Api Controllers. The filter is only hit when an error occurs in the MVC pipeline (there's MVC and Web API Controllers in the project).

I have checked this question too, but my config file seems OK:

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

    [...]

   <location path="." inheritInChildApplications="false">
      <system.web>
          <httpModules>
              <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
              <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
              <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
          </httpModules>
          [..]
      </system.web>
   </location>

   [...]

   <system.webServer>
       <modules runAllManagedModulesForAllRequests="true">
           <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
           <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />
           <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" />
       </modules>
  </system.webServer>

The filter in Global.asax:

public void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)
{
    var ctx = e.Context as HttpContext;

    if (ctx == null) return;

    ElmahDataFilter.Apply(e, ctx);
}

As additional info, if I comment the system.web>httpModules part, the log are still being created normally.

Jean Lourenço
  • 675
  • 6
  • 17

1 Answers1

5

ELMAH doesn't log errors generated by ASP.NET Web API out of the box. You need to install the Elmah.Contrib.WebApi packages as well:

Install-Package Elmah.Contrib.WebApi

Then configure Web API to use ELMAH as exception logger in your WebApiConfig.cs:

config.Services.Add(typeof(IExceptionLogger), new ElmahExceptionLogger());

For more information, check out the guide Logging to ELMAH from Web API that I've written. In the tutorial you will need to replace the elmah.io package with ELMAH, since you are probably using one of the other error loggers.

I just verified that installing these packages, actually triggers the ErrorLog_Filtering code that you have added to your question.

ThomasArdal
  • 4,999
  • 4
  • 33
  • 73
  • That's it, thanks. A quick question: on my filter, i'm calling 'e.Dismiss()', but two log files are being created still. There's another way to supress the log creation? (I'm basically folowing [this steps](https://stackoverflow.com/questions/6628389/in-elmah-with-mvc-3-how-can-i-hide-sensitive-form-data-from-the-error-log)) – Jean Lourenço Aug 16 '16 at 19:19
  • Is the error still logged, even though you dismiss it or is it some log files you are referring to? – ThomasArdal Aug 16 '16 at 19:20
  • The error is still logged even though dismissed. I need it dismissed because I create it manually `ErrorLog.GetDefault(null).Log(error);` so It only shows the data I want. – Jean Lourenço Aug 16 '16 at 19:24
  • 1
    I've previously written and tested this solution: http://docs.elmah.io/remove-sensitive-form-data/ – ThomasArdal Aug 16 '16 at 19:26
  • Yep, It's working. The problem was another service of the application writing the same logs when an exception was thrown. Thanks again for the help :) – Jean Lourenço Aug 16 '16 at 20:15
  • No problem at all. – ThomasArdal Aug 16 '16 at 20:19