Last active
December 17, 2015 20:58
-
-
Save danielgreen/5671024 to your computer and use it in GitHub Desktop.
ELMAH.MVC is a useful NuGet package that provides an HTTP module to automatically log unhandled ("Yellow Screen Of Death") exceptions in your website, and provides a page (elmah.axd) to allow the exceptions to be reviewed. However, your MVC project is likely to use the built-in HandleErrorAttribute filter which, when custom errors are enabled, c…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
ELMAH - Error Logging Modules and Handlers for ASP.NET | |
Copyright (c) 2004-9 Atif Aziz. All rights reserved. | |
Author(s): | |
Atif Aziz, http://www.raboof.com | |
Phil Haacked, http://haacked.com | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
*/ | |
-- ELMAH DDL script for Microsoft SQL Server 2000 or later. | |
-- $Id: SQLServer.sql 677 2009-09-29 18:02:39Z azizatif $ | |
DECLARE @DBCompatibilityLevel INT | |
DECLARE @DBCompatibilityLevelMajor INT | |
DECLARE @DBCompatibilityLevelMinor INT | |
SELECT | |
@DBCompatibilityLevel = cmptlevel | |
FROM | |
master.dbo.sysdatabases | |
WHERE | |
name = DB_NAME() | |
IF @DBCompatibilityLevel <> 80 | |
BEGIN | |
SELECT @DBCompatibilityLevelMajor = @DBCompatibilityLevel / 10, | |
@DBCompatibilityLevelMinor = @DBCompatibilityLevel % 10 | |
PRINT N' | |
=========================================================================== | |
WARNING! | |
--------------------------------------------------------------------------- | |
This script is designed for Microsoft SQL Server 2000 (8.0) but your | |
database is set up for compatibility with version ' | |
+ CAST(@DBCompatibilityLevelMajor AS NVARCHAR(80)) | |
+ N'.' | |
+ CAST(@DBCompatibilityLevelMinor AS NVARCHAR(80)) | |
+ N'. Although | |
the script should work with later versions of Microsoft SQL Server, | |
you can ensure compatibility by executing the following statement: | |
ALTER DATABASE [' | |
+ DB_NAME() | |
+ N'] | |
SET COMPATIBILITY_LEVEL = 80 | |
If you are hosting ELMAH in the same database as your application | |
database and do not wish to change the compatibility option then you | |
should create a separate database to host ELMAH where you can set the | |
compatibility level more freely. | |
If you continue with the current setup, please report any compatibility | |
issues you encounter over at: | |
http://code.google.com/p/elmah/issues/list | |
=========================================================================== | |
' | |
END | |
GO | |
/* ------------------------------------------------------------------------ | |
TABLES | |
------------------------------------------------------------------------ */ | |
CREATE TABLE [dbo].[ELMAH_Error] | |
( | |
[ErrorId] UNIQUEIDENTIFIER NOT NULL, | |
[Application] NVARCHAR(60) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, | |
[Host] NVARCHAR(50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, | |
[Type] NVARCHAR(100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, | |
[Source] NVARCHAR(60) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, | |
[Message] NVARCHAR(500) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, | |
[User] NVARCHAR(50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, | |
[StatusCode] INT NOT NULL, | |
[TimeUtc] DATETIME NOT NULL, | |
[Sequence] INT IDENTITY (1, 1) NOT NULL, | |
[AllXml] NTEXT COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL | |
) | |
ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] | |
GO | |
ALTER TABLE [dbo].[ELMAH_Error] WITH NOCHECK ADD | |
CONSTRAINT [PK_ELMAH_Error] PRIMARY KEY NONCLUSTERED ([ErrorId]) ON [PRIMARY] | |
GO | |
ALTER TABLE [dbo].[ELMAH_Error] ADD | |
CONSTRAINT [DF_ELMAH_Error_ErrorId] DEFAULT (NEWID()) FOR [ErrorId] | |
GO | |
CREATE NONCLUSTERED INDEX [IX_ELMAH_Error_App_Time_Seq] ON [dbo].[ELMAH_Error] | |
( | |
[Application] ASC, | |
[TimeUtc] DESC, | |
[Sequence] DESC | |
) | |
ON [PRIMARY] | |
GO | |
/* ------------------------------------------------------------------------ | |
STORED PROCEDURES | |
------------------------------------------------------------------------ */ | |
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 @FirstTimeUTC DATETIME | |
DECLARE @FirstSequence INT | |
DECLARE @StartRow INT | |
DECLARE @StartRowIndex INT | |
SELECT | |
@TotalCount = COUNT(1) | |
FROM | |
[ELMAH_Error] | |
WHERE | |
[Application] = @Application | |
-- Get the ID of the first error for the requested page | |
SET @StartRowIndex = @PageIndex * @PageSize + 1 | |
IF @StartRowIndex <= @TotalCount | |
BEGIN | |
SET ROWCOUNT @StartRowIndex | |
SELECT | |
@FirstTimeUTC = [TimeUtc], | |
@FirstSequence = [Sequence] | |
FROM | |
[ELMAH_Error] | |
WHERE | |
[Application] = @Application | |
ORDER BY | |
[TimeUtc] DESC, | |
[Sequence] DESC | |
END | |
ELSE | |
BEGIN | |
SET @PageSize = 0 | |
END | |
-- Now set the row count to the requested page size and get | |
-- all records below it for the pertaining application. | |
SET ROWCOUNT @PageSize | |
SELECT | |
errorId = [ErrorId], | |
application = [Application], | |
host = [Host], | |
type = [Type], | |
source = [Source], | |
message = [Message], | |
[user] = [User], | |
statusCode = [StatusCode], | |
time = CONVERT(VARCHAR(50), [TimeUtc], 126) + 'Z' | |
FROM | |
[ELMAH_Error] error | |
WHERE | |
[Application] = @Application | |
AND | |
[TimeUtc] <= @FirstTimeUTC | |
AND | |
[Sequence] <= @FirstSequence | |
ORDER BY | |
[TimeUtc] DESC, | |
[Sequence] DESC | |
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net; | |
using System.Text; | |
using System.Web; | |
using System.Web.Mvc; | |
using Elmah; | |
namespace Web.Filters | |
{ | |
public class ELMAHHandleErrorAttribute : HandleErrorAttribute | |
{ | |
public override void OnException(ExceptionContext context) | |
{ | |
// Set TrySkipIisCustomErrors = true to tell IIS not to interfere | |
context.HttpContext.Response.TrySkipIisCustomErrors = true; | |
if (context.HttpContext.Request.IsAjaxRequest()) | |
{ | |
context.Result = new ContentResult | |
{ | |
Content = context.Exception.Message, | |
ContentEncoding = System.Text.Encoding.UTF8, | |
ContentType = "text/plain" | |
}; | |
context.ExceptionHandled = true; | |
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError; | |
} | |
else | |
{ | |
base.OnException(context); | |
} | |
var e = context.Exception; | |
if (!context.ExceptionHandled // if unhandled, will be logged anyhow | |
|| RaiseErrorSignal(e) // prefer signaling, if possible | |
|| IsFiltered(context)) // filtered? | |
return; | |
LogException(e); | |
} | |
// Check that elmah.mvc.disableHandleErrorFilter = "true" in web.config otherwise the exception may be logged twice | |
private static bool RaiseErrorSignal(Exception e) | |
{ | |
var context = HttpContext.Current; | |
if (context == null) | |
return false; | |
var signal = ErrorSignal.FromContext(context); | |
if (signal == null) | |
return false; | |
signal.Raise(e, context); | |
return true; | |
} | |
private static bool IsFiltered(ExceptionContext context) | |
{ | |
var config = context.HttpContext.GetSection("elmah/errorFilter") | |
as ErrorFilterConfiguration; | |
if (config == null) | |
return false; | |
var testContext = new ErrorFilterModule.AssertionHelperContext( | |
context.Exception, HttpContext.Current); | |
return config.Assertion.Test(testContext); | |
} | |
private static void LogException(Exception e) | |
{ | |
var context = HttpContext.Current; | |
ErrorLog.GetDefault(context).Log(new Error(e, context)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In ASP.NET 4.5 and onwards, this will be in App_Start\FilterConfig.cs | |
public static void RegisterGlobalFilters(GlobalFilterCollection filters) | |
{ | |
filters.Add(new ELMAHHandleErrorAttribute()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<configuration> | |
<configSections> | |
<sectionGroup name="elmah"> | |
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" /> | |
<section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" /> | |
<section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" /> | |
</sectionGroup> | |
</configSections> | |
<connectionStrings> | |
<!-- local DB connection string --> | |
<add name="Elmah.Sql" connectionString="Data Source=.;Initial Catalog=MyApplicationDB;User ID=MyUser;Password=User123" /> | |
</connectionStrings> | |
<appSettings> | |
<add key="elmah.mvc.disableHandler" value="false" /> | |
<!-- Set disableHandleErrorFilter to false as we want to use TORHandleErrorAttribute instead of ELMAH's own filter. | |
See http://stackoverflow.com/a/14507920/589827 and https://github.com/alexanderbeletsky/elmah-mvc/blob/master/src/Elmah.Mvc/HandleErrorAttribute.cs --> | |
<add key="elmah.mvc.disableHandleErrorFilter" value="true" /> | |
<add key="elmah.mvc.requiresAuthentication" value="true" /> | |
<add key="elmah.mvc.allowedRoles" value="TheDomain\Admins" /> | |
<add key="elmah.mvc.route" value="elmah" /> | |
</appSettings> | |
<system.web> | |
<httpHandlers> | |
<add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" /> | |
</httpHandlers> | |
<customErrors mode="On" redirectMode="ResponseRewrite"> | |
</customErrors> | |
<httpModules> | |
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" /> | |
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" /> | |
</httpModules> | |
</system.web> | |
<system.webServer> | |
<validation validateIntegratedModeConfiguration="false" /> | |
<modules runAllManagedModulesForAllRequests="true"> | |
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" /> | |
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" /> | |
</modules> | |
<handlers> | |
<add name="Elmah" verb="POST,GET,HEAD" path="elmah.axd" preCondition="integratedMode" type="Elmah.ErrorLogPageFactory, Elmah" /> | |
</handlers> | |
</system.webServer> | |
<elmah> | |
<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="Elmah.Sql" /> | |
<security allowRemoteAccess="1" /> | |
</elmah> | |
</configuration> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment