-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added IRaygunUserProvider to fetch user data
- Loading branch information
1 parent
dc7f054
commit 6e7beef
Showing
15 changed files
with
258 additions
and
283 deletions.
There are no files selected for viewing
67 changes: 59 additions & 8 deletions
67
Mindscape.Raygun4Net.AspNetCore/ApplicationBuilderExtensions.cs
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,47 +1,98 @@ | ||
#nullable enable | ||
|
||
using System; | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Mindscape.Raygun4Net.AspNetCore; | ||
|
||
public static class ApplicationBuilderExtensions | ||
{ | ||
private const string NoApiKeyWarning = "Raygun API Key is not set, please set an API Key in the RaygunSettings."; | ||
|
||
/// <summary> | ||
/// Checks to see if you have an API Key and registers the Raygun Middleware. If no API Key is found, a warning will be logged. | ||
/// </summary> | ||
public static IApplicationBuilder UseRaygun(this IApplicationBuilder app) | ||
{ | ||
var settings = app.ApplicationServices.GetService<RaygunSettings>(); | ||
|
||
if (settings?.ApiKey == null) | ||
{ | ||
var logger = app.ApplicationServices.GetService<ILoggerFactory>()?.CreateLogger<RaygunMiddleware>(); | ||
|
||
if (logger != null) | ||
{ | ||
logger.LogWarning(NoApiKeyWarning); | ||
} | ||
else | ||
{ | ||
Console.WriteLine(NoApiKeyWarning); | ||
} | ||
} | ||
|
||
return app.UseMiddleware<RaygunMiddleware>(); | ||
} | ||
|
||
public static IServiceCollection AddRaygun(this IServiceCollection services, IConfiguration configuration, Action<RaygunSettings>? configure = null) | ||
/// <summary> | ||
/// Registers the Raygun Client and Raygun Settings with the DI container. Settings will be fetched from the appsettings.json file, | ||
/// and can be overridden by providing a custom configuration delegate. | ||
/// </summary> | ||
public static IServiceCollection AddRaygun(this IServiceCollection services, IConfiguration configuration, Action<RaygunSettings>? options = 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); | ||
options?.Invoke(settings); | ||
|
||
services.TryAddSingleton(settings); | ||
services.TryAddSingleton(s => new RaygunClient(s.GetService<RaygunSettings>())); | ||
services.TryAddSingleton(s => new RaygunClient(s.GetService<RaygunSettings>(), s.GetService<IRaygunUserProvider>())); | ||
services.TryAddSingleton<IRaygunUserProvider, DefaultRaygunUserProvider>(); | ||
services.AddHttpContextAccessor(); | ||
|
||
return services; | ||
} | ||
|
||
public static IServiceCollection AddRaygun(this IServiceCollection services, Action<RaygunSettings>? configure = null) | ||
/// <summary> | ||
/// Registers the Raygun Client and Raygun Settings with the DI container. Settings will be defaulted and overridden by providing a custom configuration delegate. | ||
/// </summary> | ||
public static IServiceCollection AddRaygun(this IServiceCollection services, Action<RaygunSettings>? options) | ||
{ | ||
// Fetch settings from configuration or use default settings | ||
// Since we are not using IConfiguration, we need to create a new instance of RaygunSettings | ||
var settings = new RaygunSettings(); | ||
|
||
// Override settings with user-provided settings | ||
configure?.Invoke(settings); | ||
options?.Invoke(settings); | ||
|
||
services.TryAddSingleton(settings); | ||
services.TryAddSingleton(s => new RaygunClient(s.GetService<RaygunSettings>())); | ||
services.TryAddSingleton(s => new RaygunClient(s.GetService<RaygunSettings>(), s.GetService<IRaygunUserProvider>())); | ||
services.TryAddSingleton<IRaygunUserProvider, DefaultRaygunUserProvider>(); | ||
services.AddHttpContextAccessor(); | ||
|
||
return services; | ||
} | ||
|
||
/// <summary> | ||
/// Registers a custom User Provider with the DI container. This allows you to provide your own implementation of IRaygunUserProvider. | ||
/// </summary> | ||
public static IServiceCollection AddRaygunUserProvider<T>(this IServiceCollection services) where T : class, IRaygunUserProvider | ||
{ | ||
// In case the default or any other user provider is already registered, remove it first | ||
var existing = services.FirstOrDefault(x => x.ServiceType == typeof(IRaygunUserProvider)); | ||
|
||
if (existing != null) | ||
{ | ||
services.Remove(existing); | ||
} | ||
|
||
// Add the new user provider | ||
services.TryAddSingleton<IRaygunUserProvider, T>(); | ||
|
||
return services; | ||
} | ||
} |
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
42 changes: 42 additions & 0 deletions
42
Mindscape.Raygun4Net.AspNetCore/DefaultRaygunUserProvider.cs
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,42 @@ | ||
#nullable enable | ||
|
||
using System.Security.Claims; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Mindscape.Raygun4Net.AspNetCore; | ||
|
||
public class DefaultRaygunUserProvider : IRaygunUserProvider | ||
{ | ||
private readonly IHttpContextAccessor _contextAccessor; | ||
|
||
public DefaultRaygunUserProvider(IHttpContextAccessor contextAccessor) | ||
{ | ||
_contextAccessor = contextAccessor; | ||
} | ||
|
||
public RaygunIdentifierMessage? GetUser() | ||
{ | ||
var ctx = _contextAccessor.HttpContext; | ||
|
||
if (ctx == null) | ||
{ | ||
return null; | ||
} | ||
|
||
var identity = ctx.User.Identity as ClaimsIdentity; | ||
|
||
if (identity?.IsAuthenticated == true) | ||
{ | ||
var email = identity.FindFirst(ClaimTypes.Email)?.Value ?? identity.Name; | ||
|
||
return new RaygunIdentifierMessage(email) | ||
{ | ||
IsAnonymous = false, | ||
Email = email, | ||
FullName = identity.Name | ||
}; | ||
} | ||
|
||
return 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
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,8 @@ | ||
#nullable enable | ||
|
||
namespace Mindscape.Raygun4Net; | ||
|
||
public interface IRaygunUserProvider | ||
{ | ||
public RaygunIdentifierMessage? GetUser(); | ||
} |
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
Oops, something went wrong.