MindscapeHQ / serilog-sinks-raygun

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

Implementation of RaygunSinkV2 for better registration with existing Raygun4Net providers #71

Closed phillip-haydon closed 3 weeks ago

phillip-haydon commented 1 month ago

Previously you would need to create a custom provider to resolve the RaygunClient, but registering the sink requires a string ApiKey parameter, this made registration a bit fiddly and verbose.

The new Sink registration allows for the following configurations to be done:

Example of setup for ASP.NET Applications:

using Mindscape.Raygun4Net.AspNetCore;
using Serilog;

var builder = WebApplication.CreateBuilder(args);

// Add Raygun
builder.Services.AddRaygun(builder.Configuration);
builder.Services.AddRaygunUserProvider();

builder.Host.UseSerilog((context, provider, config) =>
{
    // Add the Raygun sink
    config.WriteTo.Raygun(raygunClient: provider.GetRequiredService<RaygunClient>());
});

Example of setup for Console/Service:

using Mindscape.Raygun4Net;
using Serilog;
using Serilog.Sinks.Raygun.Extensions;

var host = Host.CreateDefaultBuilder(args)
    .ConfigureServices((context, services) =>
    {
        // Add Raygun
        services.AddRaygun(context.Configuration);
        services.AddHostedService<Worker>();
    })
    .UseSerilog((_, serviceProvider, config) =>
    {
        // Add the Raygun sink
        config.WriteTo.Raygun(raygunClient: serviceProvider.GetRequiredService<RaygunClient>());
    })
    .Build();

await host.RunAsync();

Example of setup for MAUI:

using Serilog;

var builder = MauiApp.CreateBuilder();

builder
    .UseMauiApp<App>()

    // Add Raygun
    .AddRaygun();

var app = builder.Build();

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Debug()

    // Add the Raygun sink
    .WriteTo.Raygun(raygunClient: app.Services.GetRequiredService<RaygunMauiClient>())
    .CreateLogger();

return app;