-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
37 changed files
with
905 additions
and
1,054 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
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 |
---|---|---|
@@ -1,49 +1,47 @@ | ||
using System; | ||
using System.Net; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace RaygunCore.Services | ||
namespace RaygunCore.Services; | ||
|
||
/// <summary> | ||
/// Rejects errors for local requests if <see cref="RaygunOptions.IgnoreLocalErrors"/>. | ||
/// </summary> | ||
public class LocalValidator : IRaygunValidator | ||
{ | ||
/// <summary> | ||
/// Rejects errors for local requests if <see cref="RaygunOptions.IgnoreLocalErrors"/>. | ||
/// </summary> | ||
public class LocalValidator : IRaygunValidator | ||
{ | ||
readonly IHttpContextAccessor _httpContextAccessor; | ||
readonly RaygunOptions _options; | ||
readonly IHttpContextAccessor _httpContextAccessor; | ||
readonly RaygunOptions _options; | ||
|
||
public LocalValidator(IHttpContextAccessor httpContextAccessor, IOptions<RaygunOptions> options) | ||
{ | ||
_httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor)); | ||
_options = options.Value; | ||
} | ||
public LocalValidator(IHttpContextAccessor httpContextAccessor, IOptions<RaygunOptions> options) | ||
{ | ||
_httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor)); | ||
_options = options.Value; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public bool ShouldSend(string message, Exception? ex) | ||
{ | ||
var context = _httpContextAccessor.HttpContext; | ||
return context == null || !_options.IgnoreLocalErrors || !context.Request.IsLocal(); | ||
} | ||
/// <inheritdoc/> | ||
public bool ShouldSend(string message, Exception? ex) | ||
{ | ||
var context = _httpContextAccessor.HttpContext; | ||
return context == null || !_options.IgnoreLocalErrors || !context.Request.IsLocal(); | ||
} | ||
} | ||
|
||
internal static class HttpRequestExtensions | ||
internal static class HttpRequestExtensions | ||
{ | ||
/// <summary> | ||
/// Returns true if the IP address of the request originator was 127.0.0.1 or if the IP address of the request was the same as the server's IP address. | ||
/// </summary> | ||
/// <remarks> | ||
/// Credit to Filip W for the initial implementation of this method. | ||
/// See http://www.strathweb.com/2016/04/request-islocal-in-asp-net-core/ | ||
/// </remarks> | ||
public static bool IsLocal(this HttpRequest request) | ||
{ | ||
/// <summary> | ||
/// Returns true if the IP address of the request originator was 127.0.0.1 or if the IP address of the request was the same as the server's IP address. | ||
/// </summary> | ||
/// <remarks> | ||
/// Credit to Filip W for the initial implementation of this method. | ||
/// See http://www.strathweb.com/2016/04/request-islocal-in-asp-net-core/ | ||
/// </remarks> | ||
public static bool IsLocal(this HttpRequest request) | ||
{ | ||
var connection = request.HttpContext.Connection; | ||
if (connection.RemoteIpAddress != null) | ||
return connection.RemoteIpAddress.Equals(connection.LocalIpAddress) || IPAddress.IsLoopback(connection.RemoteIpAddress); | ||
var connection = request.HttpContext.Connection; | ||
if (connection.RemoteIpAddress != null) | ||
return connection.RemoteIpAddress.Equals(connection.LocalIpAddress) || IPAddress.IsLoopback(connection.RemoteIpAddress); | ||
|
||
// for in memory TestServer or when dealing with default connection info | ||
return connection.RemoteIpAddress == null && connection.LocalIpAddress == null; | ||
} | ||
// for in memory TestServer or when dealing with default connection info | ||
return connection.RemoteIpAddress == null && connection.LocalIpAddress == null; | ||
} | ||
} |
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,38 +1,35 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace RaygunCore.Services | ||
namespace RaygunCore.Services; | ||
|
||
/// <summary> | ||
/// Catches pipeline errors and sends them to Raygun. | ||
/// </summary> | ||
public class RaygunMiddleware | ||
{ | ||
/// <summary> | ||
/// Catches pipeline errors and sends them to Raygun. | ||
/// </summary> | ||
public class RaygunMiddleware | ||
readonly RequestDelegate _next; | ||
readonly IRaygunClient _client; | ||
readonly bool _ignoreCanceledErros; | ||
|
||
public RaygunMiddleware(RequestDelegate next, IRaygunClient client, IOptions<RaygunOptions> options) | ||
{ | ||
readonly RequestDelegate _next; | ||
readonly IRaygunClient _client; | ||
readonly bool _ignoreCanceledErros; | ||
_next = next ?? throw new ArgumentNullException(nameof(next)); | ||
_client = client ?? throw new ArgumentNullException(nameof(client)); | ||
_ignoreCanceledErros = options.Value.IgnoreCanceledErrors; | ||
} | ||
|
||
public RaygunMiddleware(RequestDelegate next, IRaygunClient client, IOptions<RaygunOptions> options) | ||
public async Task Invoke(HttpContext httpContext) | ||
{ | ||
try | ||
{ | ||
_next = next ?? throw new ArgumentNullException(nameof(next)); | ||
_client = client ?? throw new ArgumentNullException(nameof(client)); | ||
_ignoreCanceledErros = options.Value.IgnoreCanceledErrors; | ||
await _next.Invoke(httpContext); | ||
} | ||
|
||
public async Task Invoke(HttpContext httpContext) | ||
catch (Exception ex) | ||
{ | ||
try | ||
{ | ||
await _next.Invoke(httpContext); | ||
} | ||
catch (Exception ex) | ||
{ | ||
if (!_ignoreCanceledErros || !(ex is OperationCanceledException)) | ||
await _client.SendAsync(ex, RaygunSeverity.Critical); | ||
throw; | ||
} | ||
if (!_ignoreCanceledErros || ex is not OperationCanceledException) | ||
await _client.SendAsync(ex, RaygunSeverity.Critical); | ||
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 |
---|---|---|
@@ -1,22 +1,20 @@ | ||
using System; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
|
||
namespace RaygunCore.Services | ||
namespace RaygunCore.Services; | ||
|
||
/// <summary> | ||
/// Registers <see cref="RaygunMiddleware"/> to capture pipeline errors and send them to Raygun. | ||
/// </summary> | ||
public class RaygunStartupFilter : IStartupFilter | ||
{ | ||
/// <summary> | ||
/// Registers <see cref="RaygunMiddleware"/> to capture pipeline errors and send them to Raygun. | ||
/// </summary> | ||
public class RaygunStartupFilter : IStartupFilter | ||
/// <inheritdoc/> | ||
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next) | ||
{ | ||
/// <inheritdoc/> | ||
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next) | ||
return app => | ||
{ | ||
return app => | ||
{ | ||
app.UseMiddleware<RaygunMiddleware>(); | ||
next(app); | ||
}; | ||
} | ||
app.UseMiddleware<RaygunMiddleware>(); | ||
next(app); | ||
}; | ||
} | ||
} |
Oops, something went wrong.