Skip to content

Latest commit

 

History

History
79 lines (60 loc) · 3.17 KB

README-ENRICHER.md

File metadata and controls

79 lines (60 loc) · 3.17 KB

ASP.NET Core Enricher

With the release of v7.3.0, the ASP.NET Core Enricher has been removed from this package. This is because the enricher had a dependency on Mindscape.Raygun4Net.AspNetCore, which in turn had a dependency on Microsoft.AspNetCore.App. Attempting to use Serilog.Sinks.Raygun in a MAUI app would result in an error.

If you still wish to use the enricher you can follow the steps outlined below.

Create a new Enricher class

Note: You will need to include a dependency on Mindscape.Raygun4Net.AspNetCore for this to work.

public class RaygunClientHttpEnricher : ILogEventEnricher
{
    private const string RaygunRequestMessagePropertyName = "RaygunSink_RequestMessage";
    private const string RaygunResponseMessagePropertyName = "RaygunSink_ResponseMessage";

    private readonly IHttpContextAccessor _httpContextAccessor;
    private readonly LogEventLevel _restrictedToMinimumLevel;
    private readonly RaygunSettings _raygunSettings; 

    public RaygunClientHttpEnricher(IHttpContextAccessor? httpContextAccessor = null, LogEventLevel restrictedToMinimumLevel = LogEventLevel.Error, RaygunSettings? raygunSettings = null)
    {
        _httpContextAccessor = httpContextAccessor ?? new HttpContextAccessor();
        _restrictedToMinimumLevel = restrictedToMinimumLevel;
        
        _raygunSettings = raygunSettings ?? new RaygunSettings();
    }

    public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
    {
        if (logEvent.Level < _restrictedToMinimumLevel)
        {
            return;
        }

        if (_httpContextAccessor.HttpContext == null)
        {
            return;
        }

        var httpRequestMessage = RaygunAspNetCoreRequestMessageBuilder
            .Build(_httpContextAccessor.HttpContext, _raygunSettings)
            .GetAwaiter()
            .GetResult();

        var httpResponseMessage = RaygunAspNetCoreResponseMessageBuilder.Build(_httpContextAccessor.HttpContext);

        // The Raygun request/response messages are stored in the logEvent properties collection.
        // When the error is sent to Raygun, these messages are extracted from the known properties
        // and then removed in order to not duplicate data in the payload.
        logEvent.AddOrUpdateProperty(propertyFactory.CreateProperty(RaygunRequestMessagePropertyName, httpRequestMessage, true));
        logEvent.AddOrUpdateProperty(propertyFactory.CreateProperty(RaygunResponseMessagePropertyName, httpResponseMessage, true));
    }
}

Include the enricher into Serilog configuration

Please note, this an example configuration to show the use of Enrich.With(...)

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Debug()
    .Enrich.With(new RaygunClientHttpEnricher())
    .WriteTo.Console()
    .WriteTo.Raygun("*your api key*")
    .CreateLogger();

Ensure HttpContextAccessor is registered with the DI container

When setting up the DI container, ensure that the HttpContextAccessor is registered.

services.AddHttpContextAccessor();

When errors are thrown they should now contain the original information from the Raygun Enricher.