Skip to content

Commit

Permalink
Updated for .NET 6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
anfomin committed Mar 29, 2022
1 parent 9a20b41 commit 5809451
Show file tree
Hide file tree
Showing 37 changed files with 905 additions and 1,054 deletions.
33 changes: 16 additions & 17 deletions RaygunCore.AspNetCore/HttpServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,25 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace Microsoft.Extensions.DependencyInjection
namespace Microsoft.Extensions.DependencyInjection;

/// <summary>
/// <see cref="IServiceCollection"/> extension methods for Raygun services registration.
/// </summary>
public static class RaygunHttpServiceExtensions
{
/// <summary>
/// <see cref="IServiceCollection"/> extension methods for Raygun services registration.
/// Registers Raygun message providers for <see cref="HttpContext"/>.
/// </summary>
public static class RaygunHttpServiceExtensions
public static IRaygunBuilder WithHttp(this IRaygunBuilder builder)
{
/// <summary>
/// Registers Raygun message providers for <see cref="HttpContext"/>.
/// </summary>
public static IRaygunBuilder WithHttp(this IRaygunBuilder builder)
{
builder.Services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
builder.Services.TryAddEnumerable(ServiceDescriptor.Transient<IRaygunMessageProvider, RequestMessageProvider>());
builder.Services.TryAddEnumerable(ServiceDescriptor.Transient<IRaygunMessageProvider, ResponseMessageProvider>());
builder.Services.TryAddEnumerable(ServiceDescriptor.Transient<IRaygunMessageProvider, UserMessageProvider>());
builder.Services.TryAddEnumerable(ServiceDescriptor.Transient<IRaygunValidator, LocalValidator>());
if (!builder.IsLogging)
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IStartupFilter, RaygunStartupFilter>());
return builder;
}
builder.Services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
builder.Services.TryAddEnumerable(ServiceDescriptor.Transient<IRaygunMessageProvider, RequestMessageProvider>());
builder.Services.TryAddEnumerable(ServiceDescriptor.Transient<IRaygunMessageProvider, ResponseMessageProvider>());
builder.Services.TryAddEnumerable(ServiceDescriptor.Transient<IRaygunMessageProvider, UserMessageProvider>());
builder.Services.TryAddEnumerable(ServiceDescriptor.Transient<IRaygunValidator, LocalValidator>());
if (!builder.IsLogging)
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IStartupFilter, RaygunStartupFilter>());
return builder;
}
}
2 changes: 1 addition & 1 deletion RaygunCore.AspNetCore/RaygunCore.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<AssemblyName>RaygunCore.AspNetCore</AssemblyName>
<Description>Raygun provider for ASP.NET Core.</Description>
<PackageTags>raygun;core;netcore;aspnetcore;aspnetcoremvc</PackageTags>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
70 changes: 34 additions & 36 deletions RaygunCore.AspNetCore/Services/LocalValidator.cs
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;
}
}
51 changes: 24 additions & 27 deletions RaygunCore.AspNetCore/Services/RaygunMiddleware.cs
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;
}
}
}
26 changes: 12 additions & 14 deletions RaygunCore.AspNetCore/Services/RaygunStartupFilter.cs
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);
};
}
}
Loading

0 comments on commit 5809451

Please sign in to comment.