Garnet.Serilog.Enricher.HttpContext 0.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package Garnet.Serilog.Enricher.HttpContext --version 0.0.3
NuGet\Install-Package Garnet.Serilog.Enricher.HttpContext -Version 0.0.3
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="0.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Garnet.Serilog.Enricher.HttpContext --version 0.0.3
#r "nuget: Garnet.Serilog.Enricher.HttpContext, 0.0.3"
#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=0.0.3

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

Garnet Serilog HttpContext Enricher Nuget Nuget

Serilog comprehensive HttpContext enrichers like request body, response body, request and response headers, user claims, ...


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
...;
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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 238 11/29/2023
0.0.4 598 5/13/2023
0.0.3 125 5/13/2023
0.0.2 267 4/12/2022