
August 30, 2011 05:16 by
Jervis
In this article, I will show you how to configure Elmah in MVC 3.0 Application. Elmah is Open source code so you also download a code also for any modification. Below is the URL for Elmah code and download.
http://code.google.com/p/elmah/
Along with this kind of feature they are providing good UI also to view all log with details along with download functionality. Elmah can be fit in any .net application with below error log mechanism.
1. Log in XML file
2. Log in SQL database
3. Log in Mail (sent an email to provided email address)
4. Log in Oracle database
5. Log in Access database
So, let we start this tutorial.
Select Project > Click on Right Click > Select “Add Library Package Reference” and search elmah and install that in your application
Now you have to add below xml tag in your web.config file.
Add below ConfigSection (if tag exist then add tag in that existing tag)
Now Add Elmah tag in below this configSection. Where you can uncomment what ever you want and add your setting.(like database name, log file path etc…)
<elmah>
<!--
Use to log errors to Microsoft SQL Server 2000 or 2005
using ASP.NET 2.0. Set value of connectionStringName attribute
to the name of the connection string settings to use from
the <connectionStrings> section.
<errorLog type="Elmah.SqlErrorLog, Elmah"
connectionStringName="..." />
-->
<!--
Use to log errors to Microsoft SQL Server 2000 or 2005
using ASP.NET 1.x and adjust the value of connectionString
attribute to your settings.
<errorLog type="Elmah.SqlErrorLog, Elmah"
connectionString="Data Source=.;Initial Catalog=ELMAH;Trusted_Connection=True" />
-->
<!--
Use to log errors to MySQL 5.0+ Set value of connectionStringName attribute
to the name of the connection string settings to use from the <connectionStrings>
section.
<errorLog type="Elmah.MySqlErrorLog, Elmah"
connectionString="..." />
-->
<!--
Use to log errors to a SQL Server Compact 4 database file (requires .NET 3.5 SP1)
Set value of connectionStringName attribute to
the name of the connection string settings to
use from the <connectionStrings> section.
<errorLog type="Elmah.SqlServerCompactErrorLog, Elmah" connectionStringName="..." />
The format of the connection string should be as follows:
<connectionStrings>
<add name="..." connectionString="data source=[path to the database file]" />
</connectionStrings>
Replace the content of the brackets with a file name, for example:
data source=C:\Elmah.sdf
If the database file doesn't exist it is created automatically.
You can also place the file in the App_Data folder, by using the |DataDirectory| macro:
<connectionStrings>
<add name="..." connectionString="data source=|DataDirectory|\Elmah.sdf" />
</connectionStrings>
-->
<!--
Use to log errors to SQLite using ASP.NET 2.0.
Set value of connectionStringName attribute to
the name of the connection string settings to
use from the <connectionStrings> section.
<errorLog type="Elmah.SQLiteErrorLog, Elmah" connectionStringName="..." />
The format of the connection string should be as follows:
<connectionStrings>
<add name="..." connectionString="data source=[path to the database file]" />
</connectionStrings>
Replace the content of the brackets with a file name, for example:
data source=C:\Elmah.db
If the database file doesn't exist it is created automatically.
To specify a path relative to and below the application root,
start with the root operator (~) followed by a forward slash (/),
as it is common in ASP.NET applications. For example:
data source=~/App_Data/Error.db
-->
<!--
Use to log errors into separate XML files that are stored on
disk at the path specified in the logPath attribute.
-->
<errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/ErrorLog" />
<!--
Use to log errors to Oracle
using ASP.NET 2.0. Set value of connectionStringName attribute
to the name of the connection string settings to use from
the <connectionStrings> section.
The schemaOwner parameter is *optional*. Use this if the owner of the
ELMAH package (pkg_elmah$error) is not the same user that you are
using to connect to the database.
<errorLog type="Elmah.OracleErrorLog, Elmah"
schemaOwner="xxx" />
connectionStringName="..." />
-->
<!--
Use to log errors to Oracle
using ASP.NET 1.1 and adjust the value of connectionString
attribute to your settings.
The schemaOwner parameter is *optional*. Use this if the owner of the
ELMAH package (pkg_elmah$error) is not the same user that you are
using to connect to the database.
<errorLog type="Elmah.OracleErrorLog, Elmah"
schemaOwner="xxx" />
connectionString="Data Source=xxxx;User ID=username;Password=password" />
-->
<!--
Use to log errors to Microsoft Access
using ASP.NET 1.x and adjust the value of connectionString
attribute to your settings.
Use Elmah.mdb as your database.
<errorLog type="Elmah.AccessErrorLog, Elmah"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Elmah.mdb" />
-->
<!--
Use to log errors to Microsoft Access
using ASP.NET 2.0. Set value of connectionStringName attribute
to the name of the connection string settings to use from
the <connectionStrings> section.
Use Elmah.mdb as your database.
<errorLog type="Elmah.AccessErrorLog, Elmah"
connectionStringName="..." />
-->
<!--
Use to log errors to PostgresSQL
using ASP.NET 2.0. Set value of connectionString to a
valid Npgsql connection string.
<errorLog type="Elmah.PgsqlErrorLog, Elmah"
connectionString="Server=...;Port=...;Database=...;User Id=...;Password=...;" />
-->
<!--
Use to send error reports by e-mail and adjust the attributes
according to settings desired. Note that only the from and
to attributes are required. The rest are optional and may
be removed. If the SMTP server does not require authentication,
you MUST remove the userName and password attributes or
set them to empty values (resulting in zero-length strings).
If you are using ELMAH compiled for .NET Framework 1.x, then
the to attribute may contain multiple recipient addresses, \
each of which must be delimited by a semi-colon(;). If you are
using ELMAH compiled for .NET Framework 2.0 or later, then the
to attribute may contain multiple recipient addresses,
each of which must be delimited by a comma (,).
<errorMail
from="[email protected]"
to="[email protected]"
subject="..."
priority="Low|Normal|High"
async="true|false"
smtpPort="25"
smtpServer="smtp.example.com"
useSsl="true|false"
userName="johndoe"
password="secret"
noYsod="true|false" />
-->
<!--
Use to send short error messages to a twitter account.
<errorTweet
userName="johndoe"
password="secret" />
-->
<!--
Use to prevent errors from being mailed or logged based on
assertions. An assertion evaluates the current context, resulting
in a Boolean outcome. An error is filtered if the assertion results
in true. For a more detailed explanation, see:
http://code.google.com/p/elmah/wiki/ErrorFiltering
The example below will filter errors when any of the
following conditions occur:
- The status code is set to 404
- The root/base cause is System.IO.FileNotFoundException
- The root/base cause is System.Web.HttpRequestValidationException
- The user agent making the request identifies itself as "crawler"
- The request is from the local machine
The assertion below is expressed as a JScript expression and is the
most generic and simplest of the assertions to use if you are
familiar with the JavaScript language. It can work in Medium and
Full trust environments.
<errorFilter>
<test>
<jscript>
<expression>
<![CDATA[
// @assembly mscorlib
// @assembly System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
// @import System.IO
// @import System.Web
HttpStatusCode == 404
|| BaseException instanceof FileNotFoundException
|| BaseException instanceof HttpRequestValidationException
/* Using RegExp below (see http://msdn.microsoft.com/en-us/library/h6e2eb7w.aspx) */
|| Context.Request.UserAgent.match(/crawler/i)
|| Context.Request.ServerVariables['REMOTE_ADDR'] == '127.0.0.1' // IPv4 only
]]>
</expression>
</jscript>
</test>
</errorFilter>
-->