-
Notifications
You must be signed in to change notification settings - Fork 644
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5f98d5e
commit 03182fe
Showing
4 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
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
64 changes: 64 additions & 0 deletions
64
src/NuGetGallery.Services/Diagnostics/ElmahHandleErrorAttribute.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Web; | ||
using System.Web.Mvc; | ||
using Elmah; | ||
|
||
namespace NuGetGallery.Diagnostics | ||
{ | ||
/// <summary> | ||
/// Source: http://stackoverflow.com/a/779961/52749 | ||
/// </summary> | ||
public class ElmahHandleErrorAttribute : HandleErrorAttribute | ||
{ | ||
public override void OnException(ExceptionContext context) | ||
{ | ||
base.OnException(context); | ||
if (!context.ExceptionHandled // if unhandled, will be logged anyhow | ||
|| TryRaiseErrorSignal(context) // prefer signaling, if possible | ||
|| IsFiltered(context)) // filtered? | ||
return; | ||
|
||
LogException(context); | ||
} | ||
|
||
private static bool TryRaiseErrorSignal(ExceptionContext context) | ||
{ | ||
var httpContext = GetHttpContextImpl(context.HttpContext); | ||
if (httpContext == null) | ||
return false; | ||
var signal = ErrorSignal.FromContext(httpContext); | ||
if (signal == null) | ||
return false; | ||
signal.Raise(context.Exception, httpContext); | ||
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, | ||
GetHttpContextImpl(context.HttpContext)); | ||
return config.Assertion.Test(testContext); | ||
} | ||
|
||
private static void LogException(ExceptionContext context) | ||
{ | ||
var httpContext = GetHttpContextImpl(context.HttpContext); | ||
var error = new Error(context.Exception, httpContext); | ||
ErrorLog.GetDefault(httpContext).Log(error); | ||
} | ||
|
||
private static HttpContext GetHttpContextImpl(HttpContextBase context) | ||
{ | ||
return context.ApplicationInstance.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
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