Monday, 2 November 2015

How to install elmah in your web api

Have You ever been in a project where the development is almost complete and we have released our application to QA and the QA team is complaining they can't even login and you are trying to figure out what the problem is and we will be thinking, it would have been easier if we would have implemented the logging, so it is easy to debug the issue in server. ELMAH can be the face saver. You can implement logging without touching the code base and everything is configurable and it works seamlessly.

 
1.     Go to Package manager console in Visual Studio


2.     Select Default Project as project where you want to install Elmah



3.     Run the below command in the PM 

                       Install-Package Elmah.Contrib.WebApi

4.     Copy the below code in Web.Config/App.Config


Code:
<se<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>



5.     Copy the below code in <system.web> of WebConfig (if not exist add in the <configuration> section)

Code:
    <httpModules>
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
      <!--<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />-->
      <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
    </httpModules>
    <httpHandlers>
      <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
    </httpHandlers>


6.      Add below code to <system.webServer> of WebConfig (if not exist add in the <configuration> section)
 
Code:
<mo    <modules>
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />

      <!--<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />-->
    </modules>


7.     Comment below code if available
<!--<modules runAllManagedModulesForAllRequests="true" />-->


8.     Add the below code to <handlers> of <system.webServer>

Code:

<add name="ELMAH" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />


9.     Add below code to <configuration>. Please change the logPath based on where you want your log file to exist. You can also write your log in network path and also send an email to specified email address.


Code:
<elmah>
    <security allowRemoteAccess="true" />
    <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/App_Data/ELMAH_Log" />
    <!--<errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="\\xxx\yy" /> For network folder -->
    <!--<errorMail from="xxxxx@xx.com" to="xxxxx@yy.com" subject="Unhandled exceptions" async="true" />-->
  </elmah>


10.       You should see your log in elmah log in your elmah_log(based on your logpath configuration)

No comments:

Post a Comment

Build Bot using LUIS