Skip to content

Commit

Permalink
refactored raygunclient to be used as a singleton and cleaned up the …
Browse files Browse the repository at this point in the history
…dependencies for middleware to remove middleware settings
  • Loading branch information
phillip-haydon committed Mar 10, 2024
1 parent 267a668 commit 54e3b89
Show file tree
Hide file tree
Showing 25 changed files with 652 additions and 597 deletions.

This file was deleted.

6 changes: 1 addition & 5 deletions Mindscape.Raygun4Net.AspNetCore.Tests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@

builder.Services.AddRazorPages();
builder.Services.AddMvc();
builder.Services.AddRaygun(builder.Configuration, new RaygunMiddlewareSettings
{
// adds an optional example of over riding the client provider
ClientProvider = new ExampleRaygunAspNetCoreClientProvider()
});
builder.Services.AddRaygun(builder.Configuration);

// because we're using a library that uses Raygun, we need to initialize that too
RaygunClientFactory.Initialize(builder.Configuration["RaygunSettings:ApiKey"]);
Expand Down
32 changes: 32 additions & 0 deletions Mindscape.Raygun4Net.AspNetCore/ApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#nullable enable

using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace Mindscape.Raygun4Net.AspNetCore;

public static class ApplicationBuilderExtensions
{
public static IApplicationBuilder UseRaygun(this IApplicationBuilder app)
{
return app.UseMiddleware<RaygunMiddleware>();
}

public static IServiceCollection AddRaygun(this IServiceCollection services, IConfiguration? configuration = null, Action<RaygunSettings>? configure = null)
{
// Fetch settings from configuration or use default settings
var settings = configuration?.GetSection("RaygunSettings").Get<RaygunSettings>() ?? new RaygunSettings();

// Override settings with user-provided settings
configure?.Invoke(settings);

services.TryAddSingleton(settings);
services.TryAddSingleton(s => new RaygunClient(s.GetService<RaygunSettings>()));
services.AddHttpContextAccessor();

return services;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
#nullable enable

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
Expand All @@ -16,8 +18,13 @@ public class RaygunAspNetCoreRequestMessageBuilder
{
private const int MAX_RAW_DATA_LENGTH = 4096; // bytes

public static async Task<RaygunRequestMessage> Build(HttpContext context, RaygunRequestMessageOptions options)
public static async Task<RaygunRequestMessage> Build(HttpContext? context, RaygunRequestMessageOptions? options)
{
if (context == null)
{
return new RaygunRequestMessage();
}

var request = context.Request;

options = options ?? new RaygunRequestMessageOptions();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
using Microsoft.AspNetCore.Http;
#nullable enable

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;

namespace Mindscape.Raygun4Net.AspNetCore.Builders
{
public class RaygunAspNetCoreResponseMessageBuilder
{
public static RaygunResponseMessage Build(HttpContext context)
public static RaygunResponseMessage Build(HttpContext? context)
{
if (context == null)
{
return new RaygunResponseMessage();
}

var httpResponseFeature = context.Features.Get<IHttpResponseFeature>();
return new RaygunResponseMessage
{
Expand Down
31 changes: 31 additions & 0 deletions Mindscape.Raygun4Net.AspNetCore/HttpRequestExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Net;
using Microsoft.AspNetCore.Http;

namespace Mindscape.Raygun4Net.AspNetCore;

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 req)
{
var connection = req.HttpContext.Connection;
if (connection.RemoteIpAddress != null)
{
return (connection.LocalIpAddress != null && connection.RemoteIpAddress.Equals(connection.LocalIpAddress)) || IPAddress.IsLoopback(connection.RemoteIpAddress);
}

// for in memory TestServer or when dealing with default connection info
if (connection.RemoteIpAddress == null && connection.LocalIpAddress == null)
{
return true;
}

return false;
}
}
153 changes: 0 additions & 153 deletions Mindscape.Raygun4Net.AspNetCore/RaygunAspNetMiddleware.cs

This file was deleted.

Loading

0 comments on commit 54e3b89

Please sign in to comment.