MindscapeHQ / serilog-sinks-raygun

A Serilog sink that writes events to Raygun
Apache License 2.0
11 stars 20 forks source link

Allow Serilog Sink to use Custom Client Provider to feature match asp.net core #64

Closed phillip-haydon closed 12 months ago

phillip-haydon commented 1 year ago

When using Raygun with Serilog, it's not possible to use a custom client, i.e

public class CustomRaygunProvider : DefaultRaygunAspNetCoreClientProvider
{
    private static readonly string ServiceName = Process.GetCurrentProcess().ProcessName;
    private static readonly string ServiceVersion = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion ?? string.Empty;

    public override RaygunClient GetClient(RaygunSettings settings, HttpContext context)
    {
        var client = base.GetClient(settings, context);

        var email = context.User.FindFirstValue("email");

        client.UserInfo = new RaygunIdentifierMessage(email)
        {
            IsAnonymous = false,
            Identifier = context.User.FindFirstValue("sub"),
            Email = email,
            FullName = context.User.FindFirstValue("preferred_username")
        };

        client.ApplicationVersion = ServiceVersion;

        client.SendingMessage += (_, args) =>
        {
            args.Message.Details.Tags ??= new List<string>();
            args.Message.Details.Tags.Add(ServiceVersion);
            args.Message.Details.Tags.Add(ServiceName);
        };

        return client;
    }
}

This would be registered with asp .net core:

builder.Services.AddRaygun(builder.Configuration, new RaygunMiddlewareSettings
{
    ClientProvider = new CustomRaygunProvider()
});

This PR allows the configuration of the custom provider like so:

var key = context.Configuration["RaygunSettings:ApiKey"];
builder.Host.UseSerilog((context, configuration) =>
{
    configuration
        .WriteTo.Console()
        .WriteTo.Raygun(applicationKey: key, settings: new RaygunSettings
        {
            ApiKey = key
        },
        raygunAspNetCoreClientProvider: new CustomRaygunProvider());
});

This allows consistency between asp .net and serilog.