-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from mausworks/feature/aspnet
ASP.NET Core middleware
- Loading branch information
Showing
37 changed files
with
1,394 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
bin/ | ||
obj/ | ||
.cov/ | ||
.packages/ | ||
|
||
dsn.txt | ||
test/Pidget.AspNetExample/appsettings.json |
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
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,44 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Options; | ||
using Pidget.AspNet; | ||
|
||
namespace Pidget.AspNetExample | ||
{ | ||
public class OnExceptionMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
|
||
public ExceptionReportingOptions Options { get; } | ||
|
||
public OnExceptionMiddleware(RequestDelegate next, | ||
IOptions<ExceptionReportingOptions> optionsAccessor) | ||
{ | ||
_next = next; | ||
Options = optionsAccessor.Value; | ||
} | ||
|
||
public async Task Invoke(HttpContext context) | ||
{ | ||
try | ||
{ | ||
await _next(context); | ||
} | ||
catch (Exception ex) | ||
{ | ||
context.Response.StatusCode = 200; | ||
context.Response.ContentType = "text/plain"; | ||
|
||
await WriteExceptionAsync(context, ex); | ||
} | ||
} | ||
|
||
public async Task WriteExceptionAsync(HttpContext context, Exception ex) | ||
{ | ||
await context.Response.WriteAsync($"{ex}\r\n\r\n"); | ||
await context.Response.WriteAsync( | ||
$"Sentry event ID: {context.Items[ExceptionReportingMiddleware.EventIdKey]}"); | ||
} | ||
} | ||
} |
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,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Pidget.AspNet\Pidget.AspNet.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,16 @@ | ||
using Microsoft.AspNetCore; | ||
using Microsoft.AspNetCore.Hosting; | ||
|
||
namespace Pidget.AspNetExample | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
=> BuildWebHost(args).Run(); | ||
|
||
public static IWebHost BuildWebHost(string[] args) | ||
=> WebHost.CreateDefaultBuilder(args) | ||
.UseStartup<Startup>() | ||
.Build(); | ||
} | ||
} |
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,31 @@ | ||
using System; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Pidget.AspNet.Setup; | ||
|
||
namespace Pidget.AspNetExample | ||
{ | ||
public class Startup | ||
{ | ||
public IConfiguration Configuration { get; } | ||
|
||
public Startup(IConfiguration configuration) | ||
=> Configuration = configuration; | ||
|
||
public void ConfigureServices(IServiceCollection services) | ||
=> services.AddPidgetMiddleware( | ||
Configuration.GetSection("ExceptionReporting")); | ||
|
||
public void Configure(IApplicationBuilder app) | ||
{ | ||
// Additional exception handling. | ||
app.UseMiddleware<OnExceptionMiddleware>(); | ||
|
||
app.UsePidgetMiddleware(); | ||
|
||
// Throwing application code. | ||
app.Run(_ => throw new Exception("You shall not pass!")); | ||
} | ||
} | ||
} |
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,5 @@ | ||
{ | ||
"ExceptionReporting": { | ||
"Dsn": "https://PUBLIC:SECRET@sentry.io/PROJECT_ID" | ||
} | ||
} |
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,16 @@ | ||
{ | ||
"ExceptionReporting": { | ||
"Dsn": "https://a9d493018a4b46848d8315334ea897f5:8b0358b0e576424aa37bce67230c3123@sentry.io/233774", | ||
"EventIdKey": "SentryEventId", | ||
"Sanitation": { | ||
"ReplacementValue": "OMITTED", | ||
"ValuePatterns": [ | ||
"^(?:\\d[ -]*?){13,16}$" | ||
], | ||
"NamePatterns": [ | ||
".?passw.?", | ||
".?secret.?" | ||
] | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System; | ||
using System.Runtime.ExceptionServices; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Options; | ||
using Pidget.AspNet.Sanitizing; | ||
using Pidget.Client; | ||
|
||
namespace Pidget.AspNet | ||
{ | ||
public class ExceptionReportingMiddleware | ||
{ | ||
public const string EventIdKey = "SentryEventId"; | ||
|
||
public ExceptionReportingOptions Options { get; } | ||
|
||
private readonly RequestDelegate _next; | ||
|
||
private readonly SentryClient _sentryClient; | ||
|
||
public ExceptionReportingMiddleware(RequestDelegate next, | ||
IOptions<ExceptionReportingOptions> optionsAccessor, | ||
SentryClient sentryClient) | ||
{ | ||
_next = next; | ||
_sentryClient = sentryClient; | ||
Options = optionsAccessor.Value; | ||
} | ||
|
||
public async Task Invoke(HttpContext context) | ||
{ | ||
try | ||
{ | ||
await _next(context); | ||
} | ||
catch (Exception ex) | ||
{ | ||
var eventId = await CaptureAsync(ex, context); | ||
|
||
context.Items.Add(EventIdKey, eventId); | ||
ex.Data.Add(EventIdKey, eventId); | ||
|
||
SilentlyRethrow(ex); | ||
} | ||
} | ||
|
||
private async Task<string> CaptureAsync(Exception ex, HttpContext context) | ||
=> await _sentryClient.CaptureAsync(e | ||
=> BuildEvent(ex, context.Request, e)); | ||
|
||
private void BuildEvent(Exception ex, HttpRequest request, | ||
SentryEventBuilder sentryEvent) | ||
{ | ||
sentryEvent.SetException(ex); | ||
|
||
AddRequestData(sentryEvent, request); | ||
} | ||
|
||
private void AddRequestData(SentryEventBuilder sentryEvent, HttpRequest request) | ||
{ | ||
var provider = new RequestDataProvider( | ||
new RequestSanitizer(Options.Sanitation)); | ||
|
||
sentryEvent.SetRequestData(provider.GetRequestData(request)); | ||
} | ||
|
||
/// <summary> | ||
/// Re-throws the provided exception without adding to the stack trace. | ||
/// </summary> | ||
/// <param name="ex">The exception to re-throw.</param> | ||
private static void SilentlyRethrow(Exception ex) | ||
=> ExceptionDispatchInfo.Capture(ex).Throw(); | ||
} | ||
} |
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,12 @@ | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Pidget.AspNet | ||
{ | ||
public class ExceptionReportingOptions | ||
{ | ||
public string Dsn { get; set; } | ||
|
||
public SanitationOptions Sanitation { get; set; } | ||
= SanitationOptions.Default; | ||
} | ||
} |
Oops, something went wrong.