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.
- ErrorMailModule
ErrorMailModule is used for sending the details of log as an email.
- ErrorLogModule
ErrorLogModule is used for logging all the exceptions and some other details, like IP- Address, Usersname, Website Username etc.
- ErrorFilterModule
ErrorFilterModule is used to customize the exception logs.
How to use ELMAH ?
Step 1
Create a new MVC application.
- On the File menu, click New >> Project.
- 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.
- 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.
- Open NuGet Package Manager Console -
Click on Tools > NuGet Package Manager > Package Manager Console.
- Type "Install-Package elmah" and hit Enter.
- After successful installation, you will find the below screen.
- ELMAH Modules are default registered in web.config. If not, then use the below code.
- <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>
- Create an ActionMethod.
- public ActionResult Index() {
- return View();
- }
Run the Project. Now, enter the wrong URL in address bar and hit Enter. We will get 404 Error.
Now, for ELMAH Log, enter the below URL in address bar.
http://localhost:57979/elmah.axd
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.
- <elmah>
- <errorMail from="[email protected]" to="[email protected]" 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 4
Store ELMAH logs on different locations.
- 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.
- <elmah>
- <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/ElmahLog" /> </elmah>
- Store ELMAH logs in RAM.
Add the below setting in your Web.config.
- <elmah>
- <errorLog type="Elmah.MemoryErrorLog, Elmah" size="100" /> </elmah>
- Store ELMAH logs in Microsoft SQL Server.
Add the below setting in your Web.config.
- <elmah>
- <errorLog type="Elmah.SqlErrorLog, Elmah" connectionString="DBEntities" /> </elmah>
Note
Don`t forgot to add connectionString name as "DBEntities".
- <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>
All Done.. :)