Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
drewgillies committed Jun 24, 2024
1 parent 5f98d5e commit 03182fe
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/NuGetGallery.Core/NuGetGallery.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@
<PackageReference Include="NuGet.Services.Validation.Issues">
<Version>$(ServerCommonPackageVersion)</Version>
</PackageReference>
<PackageReference Include="NuGet.StrongName.elmah.corelibrary">
<Version>1.2.2</Version>
</PackageReference>
<PackageReference Include="Microsoft.Data.Services.Client">
<Version>5.8.4</Version>
</PackageReference>
Expand Down
64 changes: 64 additions & 0 deletions src/NuGetGallery.Services/Diagnostics/ElmahHandleErrorAttribute.cs
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
namespace NuGetGallery.Diagnostics
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
public sealed class SendErrorsToTelemetryAttribute : Attribute
public sealed class SendErrorsToTelemetryAttribute :
ElmahHandleErrorAttribute
{
public void OnException(ExceptionContext context)
public override void OnException(ExceptionContext context)
{
base.OnException(context);

if (context != null)
{
try
Expand Down
1 change: 1 addition & 0 deletions src/NuGetGallery/App_Start/AppActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ private static void AppPostStart(IAppConfiguration configuration)
Routes.RegisterRoutes(RouteTable.Routes, configuration.FeedOnlyMode, configuration.AdminPanelEnabled);
AreaRegistration.RegisterAllAreas();

GlobalFilters.Filters.Add(new SendErrorsToTelemetryAttribute { View = "~/Views/Errors/InternalError.cshtml" });
GlobalFilters.Filters.Add(new ReadOnlyModeErrorFilter());
GlobalFilters.Filters.Add(new AntiForgeryErrorFilter());
GlobalFilters.Filters.Add(new UserDeletedErrorFilter());
Expand Down

0 comments on commit 03182fe

Please sign in to comment.