Garnet.Serilog.Enricher.HttpContext 1.0.0

dotnet add package Garnet.Serilog.Enricher.HttpContext --version 1.0.0
NuGet\Install-Package Garnet.Serilog.Enricher.HttpContext -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Garnet.Serilog.Enricher.HttpContext" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Garnet.Serilog.Enricher.HttpContext --version 1.0.0
#r "nuget: Garnet.Serilog.Enricher.HttpContext, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Garnet.Serilog.Enricher.HttpContext as a Cake Addin
#addin nuget:?package=Garnet.Serilog.Enricher.HttpContext&version=1.0.0

// Install Garnet.Serilog.Enricher.HttpContext as a Cake Tool
#tool nuget:?package=Garnet.Serilog.Enricher.HttpContext&version=1.0.0

Garnet Serilog HttpContext Enricher Nuget Nuget

Serilog comprehensive HttpContext enrichers like request body, response body, request and response headers, user claims, ... with filter and redaction support


Installation

dotnet add package Garnet.Serilog.Enricher.HttpContext

List of Enrichers

  • Request Body
  • Request Headers
  • Request Url
  • Response Body
  • Response Headers
  • User Claims
  • Custom Property<small>: Enrich with any information from HttpContext</small>
  • Custom Property With Cache<small>: Like Custom Property with caching the result</small>

Usage

First, enrichers and their requirements need to be registered

services.AddAllGarnetHttpContextEnrichers() //Add all enrichers with default configuration

// ---OR---
//You can add each individual enricher
services.AddGarnetRequestBodyEnricher();
services.AddGarnetRequestHeadersEnricher();
services.AddGarnetRequestUrlEnricher();
services.AddGarnetResponseBodyEnricher();
services.AddGarnetResponseHeadersEnricher();
services.AddGarnetUserClaimsEnricher();

Other overloads of these methods can be used to set configuration or read configuration from appsettings.

For RequestBody and ResponseBody enrichers to work you need to use their ASP middlewares

app //IApplicantBuilder
  .UseGarnetRequestBodyEnricherMiddleware()
  .UseGarnetResponseBodyEnricherMiddleware()
  ...;

The order of using middlewares is important to have proper log information. For example if you have a middleware that produces custom responses on exception occurrence and you want to log the exact response, you should register the GarnetResponseBody middleware before that. More information

Now let Serilog to use Garnet enrichers all at once or one by one as you need.

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .UseSerilog((context, services, configuration) => configuration
            .ReadFrom.Configuration(context.Configuration)
            .ReadFrom.Services(services)
            .Enrich.FromLogContext()
            .Enrich.WithAllGarnetHttpContextEnrichers(serviceProvider) // <---
            // ---OR---
            .Enrich.WithGarnetRequestBody(serviceProvider)
            .Enrich.WithGarnetRequestHeaders(serviceProvider)
            .Enrich.WithGarnetRequestUrl(serviceProvider)
            .Enrich.WithGarnetResponseBody(serviceProvider)
            .Enrich.WithGarnetResponseHeaders(serviceProvider)
            .Enrich.WithGarnetUserClaims(serviceProvider);
            // --------
            .WriteTo.Console())
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

You can use custom enrichers to add more information to LogEvent from HttpContext

...
.Enrich.With(new GarnetHttpContextCustomPropertyEnricher("ResponseStatusCode", 
    serviceProvider,
    httpContext => httpContext.Response.StatusCode)) //<--- provide result
...;

// ---OR---

// The following custom enricher caches the provided result for further events in a same instanse of the HttpContext (in a same incoming request)
...
.Enrich.With(new GarnetHttpContextCustomPropertyEnricherWithCache("RequestMethod", 
    serviceProvider,
    httpContext => httpContext.Request.Method)); //<--- provide result
...;

Redaction and Filter

Redaction can be applied to the Request or Response content for request to predefined URLs or all. For now it only redact fields of content in Json or form-urlencoded format.

Every enrichment can be filtered for every request by using HttpContext.

serviceCollection.AddAllGarnetHttpContextEnrichers(configuration: new GarnetHttpContextEnrichmentConfiguration)
{
    //Ignore enrichment on request to swagger
    RequestFilter = context =>
        !context.Request.Path.Value.Contains("swagger", StringComparison.InvariantCultureIgnoreCase),
        
    //Redact password field on login
    Redactions = new List<Redaction>
    {
        new()
        {
            Url = "connect/token",
            Fields = new List<string> { "password" },
        }
    }
});
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 229 11/29/2023
0.0.4 590 5/13/2023
0.0.3 123 5/13/2023
0.0.2 266 4/12/2022