Steps to implement Elmah with your website.

ELMAH is an application – wide exception logging system that logs unhandled exceptions. you can add ELMAH assembly in your developing project or you can configure it and add dynamically in you running Asp.net application
Background

1.Logs all the unhandled exceptions
2.Gives custome exceptions even thought the custome exception mode is off
3.An RSS feed of the exception.
4.Email the exceptions

 Implementation
1.Add ELMAH assembly in your project.

To download elmah assembly:
http://code.google.com/p/elmah/wiki/Downloads?tm=2

Also, ELMAH is available here
2.Configur your web.config

For latest web.config configuration:
http://elmah.googlecode.com/svn-history/r761/tags/REL-1.2-BETA/samples/web.config

Here is a sample web config setting

<?xml version=”1.0″ encoding=”utf-8″?>
<configuration>
  <configSections>
    <sectionGroup name=”elmah”>
      <section name=”errorLog” requirePermission=”false” type=”Elmah.ErrorLogSectionHandler, Elmah” />
      <section name=”errorMail” requirePermission=”false” type=”Elmah.ErrorMailSectionHandler, Elmah” />
      <section name=”errorFilter” requirePermission=”false” type=”Elmah.ErrorFilterSectionHandler, Elmah”/>
    </sectionGroup>
  </configSections>
  <connectionStrings >
    <add name=”elmah-sql” connectionString=”Data Source=xxx;Initial Catalog=ELMAH;Trusted_Connection=True” />
  </connectionStrings>
  <elmah>
    <errorLog type=”Elmah.SqlErrorLog, Elmah” connectionStringName=”elmah-sql”   >
    </errorLog>
  </elmah>
  <system.web>
    <httpModules>
      <add name=”ErrorLog” type=”Elmah.ErrorLogModule, Elmah”/>
    </httpModules>
    <httpHandlers>
      <add verb=”POST,GET,HEAD” path=”elmah.axd” type=”Elmah.ErrorLogPageFactory, Elmah” />
    </httpHandlers>
    <customErrors mode=”On” defaultRedirect=”CustomeErrorPage.htm” />
  </system.web>
</configuration>

3.Script to create required tables and stored procedure

CREATE TABLE dbo.ELMAH_Error
(
    ErrorId     UNIQUEIDENTIFIER NOT NULL,
    Application NVARCHAR(60)  NOT NULL,
    Host        NVARCHAR(50)  NOT NULL,
    Type        NVARCHAR(100) NOT NULL,
    Source      NVARCHAR(60)  NOT NULL,
    Message     NVARCHAR(500) NOT NULL,
    [User]      NVARCHAR(50)  NOT NULL,
    StatusCode  INT NOT NULL,
    TimeUtc     DATETIME NOT NULL,
    Sequence    INT IDENTITY (1, 1) NOT NULL,
    AllXml      NTEXT  NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

ALTER TABLE dbo.ELMAH_Error WITH NOCHECK ADD
    CONSTRAINT PK_ELMAH_Error PRIMARY KEY
    (
        ErrorId
    )  ON [PRIMARY]
GO

ALTER TABLE dbo.ELMAH_Error ADD
    CONSTRAINT DF_ELMAH_Error_ErrorId DEFAULT (newid()) FOR [ErrorId]
GO

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE PROCEDURE dbo.ELMAH_GetErrorXml
(
    @Application NVARCHAR(60),
    @ErrorId UNIQUEIDENTIFIER
)
AS

SET NOCOUNT ON

SELECT
    AllXml
FROM
    ELMAH_Error
WHERE
    ErrorId = @ErrorId
AND
    Application = @Application

 

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE PROCEDURE dbo.ELMAH_GetErrorsXml
(
    @Application NVARCHAR(60),
    @PageIndex INT = 0,
    @PageSize INT = 15,
    @TotalCount INT OUTPUT
)
AS

SET NOCOUNT ON

DECLARE @Page TABLE
(
    Position INT IDENTITY(1, 1) NOT NULL,
    ErrorId UNIQUEIDENTIFIER NOT NULL,
    Application NVARCHAR(60) NOT NULL,
    Host NVARCHAR(30) NOT NULL,
    Type NVARCHAR(100) NOT NULL,
    Source NVARCHAR(60) NOT NULL,
    Message NVARCHAR(500) NOT NULL,
    [User] NVARCHAR(50) NOT NULL,
    StatusCode INT NOT NULL,
    TimeUtc DATETIME NOT NULL
)

INSERT
INTO
    @Page
    (
        ErrorId,
        Application,
        Host,
        Type,
        Source,
        Message,
        [User],
        StatusCode,
        TimeUtc
    )
SELECT
    ErrorId,
    Application,
    Host,
    Type,
    Source,
    Message,
    [User],
    StatusCode,
    TimeUtc
FROM
    ELMAH_Error
WHERE
    Application = @Application  �
ORDER BY
    TimeUtc DESC,
    Sequence DESC

SELECT
    @TotalCount = COUNT(*)
FROM
    @Page

DECLARE @FirstPosition INT
SET @FirstPosition = @PageIndex * @PageSize + 1

DECLARE @LastPosition INT
SET @LastPosition  = @FirstPosition + @PageSize – 1

SELECT
    errorId,
    application,
    host,
    type,
    source,
    message,
    [user],
    statusCode,
    CONVERT(VARCHAR(50), TimeUtc, 126) + ‘Z’ time
FROM
    @Page error
WHERE
    Position >= @FirstPosition
AND
    Position <= @LastPosition
ORDER BY
    Position
FOR
    XML AUTO

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE PROCEDURE dbo.ELMAH_LogError
(
    @ErrorId UNIQUEIDENTIFIER,
    @Application NVARCHAR(60),
    @Host NVARCHAR(30),
    @Type NVARCHAR(100),
    @Source NVARCHAR(60),
    @Message NVARCHAR(500),
    @User NVARCHAR(50),
    @AllXml NTEXT,
    @StatusCode INT,
    @TimeUtc DATETIME
)
AS

SET NOCOUNT ON

INSERT
INTO
    ELMAH_Error
    (
        ErrorId,
        Application,
        Host,
        Type,
        Source,
        Message,
        [User],
        AllXml,
        StatusCode,
        TimeUtc
    )
VALUES
    (
        @ErrorId,
        @Application,
        @Host,
        @Type,
        @Source,
        @Message,
        @User,
        @AllXml,
        @StatusCode,
        @TimeUtc
    )

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
In order to get the details just append “/elmah.axd to you browser to the default page.

e.g. http://localhost/sitename/elmah.axd

Thank to Mr. Gautam Sharma

http://www.codeproject.com/KB/aspnet/ELMAHDemo.aspx

 

 

ASP.NET, Visual Studio ASP.NET ,

3 responses to Steps to implement Elmah with your website.


  1. I just want to say I am just new to blogging and site-building and definitely liked you’re blog site. Probably I’m planning to bookmark your blog post . You surely have good article content. Bless you for sharing with us your web-site.

  2. I like this weblog so much, saved to my bookmarks . cpanel web hosting | cpanel web hosting |

  3. Thank you for your blog! I’ve just subscribed to your news feed.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>