Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#

Elmah in MVC

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

Introduction

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.

Background

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

ELMAH HTTP Modules.

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.

XML
<system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" />
    <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>
      <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>

<elmah>
    <security allowRemoteAccess="false" />
</elmah>

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

5) Create an ActionMethod

C#
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.

http://localhost:57979/elmah.axd

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

XML
<elmah>
     <errorMail from="suchit@elmah.com"
     to="suchit.webmyne@gmail.com"
     subject="Error - ELMAH demo - Suchit Khunt"
     async="true" />    
</elmah>

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

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

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

2) Store ELMAH logs in RAM.

-> Add below setting in your Web.config

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

3) Store ELMAH logs in Microsoft SQL Server.

-> Add below setting in your Web.config

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

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

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

HERE  ALL DONE... :)

License

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


Written By
Software Developer (Senior) Riowebs
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionError Pin
Member 104030905-Apr-17 20:41
Member 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.