Click here to Skip to main content
15,038,643 members
Articles / Programming Languages / C#
Posted 6 Dec 2016

Tagged as


12 bookmarked

Elmah in MVC

Rate me:
Please Sign up or sign in to vote.
4.97/5 (12 votes)
6 Dec 2016CPOL3 min read
What is Elmah and how to use Elmah in MVC ?


Let’s first establish what the purpose of code is in the first place.

For this article, the purpose of code is what is ELMAH and how to use & how to configaration it in MVC.


What is ELMAH ?

ELMAH = Error Logging Modules And Handlers

ELMAH provide functionality to logging runtime ASP.NET errors.

Why we choose ELMAH ?

  • ELMAH logging all unhandle exceptions.
  • ELMAH log all errors in many storage like
    • SQL Server
    • My SQL
    • Randon Access Memory (RAM)
    • SQL Lite
    • Oracle   
  • ELMAH has also functionality to download all errors in CSV file.
  • RSS feed for last 15 errors
  • Get all errors data in JSON or XML format
  • Get all errors to our mailbox
  • Send error log notification to your application
  • Customise the error log by custiomising some code


There are three HTTP Modules.

  1. ErrorMailModule
  2. ErrorLogModule
  3. ErrorFilterModule

ErrorMailModule is use for sends the details of log as an E-Mail.

ErrorLogModule is use for logs the all exception and some another details like IP-Address, Usersname, Website Username etc.

ErrorFilterModule is use to customise the exceptino logs.

Using the code

STEP:01 Create an new MVC Application.

1) On the File menu, click New then Project.

2) In the New Project dialog box under Project types, expand "Visual C#", and then click "Web" and In the Name box, type "DemoELMAH" then click on Ok.

3) Now, In the dialog box click on the "MVC" under the ASP.NET 4.5.2 Templates, Then click on "Change Authentication" which is stay on center of the right side and finally select "No Authentication" and click on ok.

STEP:02 Install ELMAH Library and Register its modules.

1) Open NuGet Package Manager Console using below.
   Click on Tools > NuGet Package Manager > Package Manager Console

2) Type "Install-Package elmah" and hit enter

Image 1

3) After successfull installation you found below screen.  

Image 2

4) ELMAH Moduls are default registered in Web.config. If not then use below code.

    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" />
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
      <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
      <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />

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

    <security allowRemoteAccess="false" />

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

5) Create an ActionMethod

public ActionResult Index()
    return View();

Run the Project. Now, Enter wrong URL in address bar and hit enter. We fonund 404 Error.

Image 3

Now, for ELMAH Log enter below URL in address bar.


Image 4

NOTE:  MVC now ignores .axd files by default so this one is not used in server. But later in this article we store the logs in another location.

STEP:03 Setup mail server for got every logs in Email. Add below code in web.config

     <errorMail from=""
     subject="Error - ELMAH demo - Suchit Khunt"
     async="true" />    

      <smtp deliveryMethod="Network">
        <network host="host address" port="port number" userName="your username" password="your password"/>

STEP:04 Store ELMAH logs in diffrent location.

1) Store ELMAH logs in XML file.

-> Create a one folder name as "ElmahLog" in root directiory of your project. We use this folder for save XML file.

-> Add below setting in your Web.config

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

2) Store ELMAH logs in RAM.

-> Add below setting in your Web.config

    <errorLog type="Elmah.MemoryErrorLog, Elmah" size="100" />

3) Store ELMAH logs in Microsoft SQL Server.

-> Add below setting in your Web.config

    <errorLog type="Elmah.SqlErrorLog, Elmah" connectionString="DBEntities" />

Note: Don`t forgot to add connectionString name as "DBEntities". Add connectionString name as "DBEntities" as per below.

    <add name="DBEntities" connectionString="data source=server name;initial catalog=database name;persist security info=True;user id=your username;password=your password;Trusted_Connection=True" />



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Suchit R. Khunt
Software Developer (Senior) Riowebs
India India
No Biography provided

Comments and Discussions

QuestionError Pin
Member 104030905-Apr-17 20:41
MemberMember 104030905-Apr-17 20:41 
GeneralMy vote of 5 Pin
Oshtri Deka15-Dec-16 1:14
professionalOshtri Deka15-Dec-16 1:14 
GeneralRe: My vote of 5 Pin
Suchit R. Khunt16-Dec-16 2:29
professionalSuchit R. Khunt16-Dec-16 2:29 
Thanks @Oshtri Deka

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.