Introduction To ELMAH In MVC

Let’s first establish what the purpose of the code is. For this article, the purpose of code is to learn about ELMAH and how to use and configure it in MVC.

What is ELMAH?


ELMAH stands for Error Logging Modules And Handlers that provide functionality to logging runtime ASP.NET errors.

Why do we choose ELMAH?

  • ELMAH enables logging of all unhandled exceptions.
  • ELMAH logs all errors in many storages, like - SQL Server, MySQL, Randon Access Memory (RAM), SQL Lite, and Oracle.
  • ELMAH has functionality to download all errors in CSV file.
  • RSS feed for the last 15 errors
  • Get all error data in JSON or XML format
  • Get all errors to our mailbox
  • Send error log notification to your application
  • Customize the error log by custiomizing some code

ELMAH HTTP Modules


There are three HTTP Modules.

  1. ErrorMailModule
    ErrorMailModule is used for sending the details of log as an email.

  2. ErrorLogModule
    ErrorLogModule is used for logging all the exceptions and some other details, like IP- Address, Usersname, Website Username etc.

  3. ErrorFilterModule
    ErrorFilterModule is used to customize the exception logs.

How to use ELMAH?


Step 1

Create a new MVC application.

  1. On the File menu, click New >> 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". Finally, click on OK.

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

Step 2

Install ELMAH Library and register its modules.

  1. Open NuGet Package Manager Console -

    Click on Tools > NuGet Package Manager > Package Manager Console.

  2. Type "Install-Package elmah" and hit Enter.

  3. After successful installation, you will find the below screen.



  4. ELMAH Modules are default registered in web.config. If not, then use the below code.
    1. <system.web>  
    2.     <compilation debug="true" targetFramework="4.5.2" />  
    3.     <httpRuntime targetFramework="4.5.2" />  
    4.     <httpModules>  
    5.         <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />  
    6.         <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />  
    7.         <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" /> </httpModules>  
    8. </system.web>  
    9. <system.webServer>  
    10.     <modules>  
    11.         <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />  
    12.         <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />  
    13.         <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" /> </modules>  
    14. </system.webServer>  
    15. <elmah>  
    16.     <security allowRemoteAccess="false" /> </elmah>  
    17. <location path="elmah.axd" inheritInChildApplications="false">  
    18.     <system.web>  
    19.         <httpHandlers>  
    20.             <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" /> </httpHandlers>  
    21.     </system.web>  
    22.     <system.webServer>  
    23.         <handlers>  
    24.             <add name="ELMAH" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" /> </handlers>  
    25.     </system.webServer>  
    26. </location>// This is just a sample script. Paste your real code (javascript or HTML) here. if ('this_is'==/an_example/){of_beautifier();}else{var a=b?(c%d):e[f];}  
  5. Create an ActionMethod.
    1. public ActionResult Index() {  
    2.     return View();  
    3. }  
    Run the Project. Now, enter the wrong URL in address bar and hit Enter. We will get 404 Error.

    successfull

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

    http://localhost:57979/elmah.axd

    successfull

    NOTE
    - MVC now ignores .axd files by default. So, this one is not used in Server. But later in this article, we will store the logs in another location.

Step 3

Setup Mail Server for getting every log in email; add the below code in web.config.

  1. <elmah>  
  2.     <errorMail from="****@elmah.com" to="***.***@gmail.com" subject="Error - ELMAH demo - ***" async="true" /> </elmah>  
  3. <system.net>  
  4.     <mailSettings>  
  5.         <smtp deliveryMethod="Network">  
  6.             <network host="host address" port="port number" userName="your username" password="your password" /> </smtp>  
  7.     </mailSettings>  
  8. </system.net>  
Step 4

Store ELMAH logs on different locations.
  1. Store ELMAH logs in XML file

    Create one folder named "ElmahLog" in root directory of your project. We use this folder for saving the XML file.

    Add the below setting in your Web.config.
    1. <elmah>  
    2.     <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/ElmahLog" /> </elmah>  
  2. Store ELMAH logs in RAM.

    Add the below setting in your Web.config.
    1. <elmah>  
    2.     <errorLog type="Elmah.MemoryErrorLog, Elmah" size="100" /> </elmah>  
  3. Store ELMAH logs in Microsoft SQL Server.

    Add the below setting in your Web.config.
    1. <elmah>  
    2.     <errorLog type="Elmah.SqlErrorLog, Elmah" connectionString="DBEntities" /> </elmah>  
    Note

    Don`t forgot to add connectionString name as "DBEntities".
    1. <connectionStrings>  
    2.     <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>  

All Done.. :)


Similar Articles