Search code examples
.netrestconsole-applicationelmah

Elmah logs in self hosted rest application


I am having a console application which invokes services from a self hosted rest api project(say http://localhost:8000/restapi).

In the rest api project, Elmah is being used to log any exceptions.

Now, when I am accessing the service from the console application, and if there are some exceptions Elmah is not logging it !

Note that when the Rest Api is being hosted in IIS, and the service is invoked from there, the Elmah logging works fine.

Can somebody please suggest ?

Config of the rest API

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

    <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"/>
    <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah"/>
    </httpModules>

    <elmah>
      <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/App_Data" />
    </elmah>

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

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

Solution

  • Got some clues from Here and there

    I had missed to configure the Error logging filter, before invoking Web API from the console application

    here are the missing bits -

    var config = HttpSelfHostConfiguration("http://localhost:8000");
    config.Filters.Add(new MyElmahErrorLoggingFilter());
    var server = new HttpSelfHostServer(config);