Serilog.Enrichers.AspNetCore.RequestHeader 1.0.3

dotnet add package Serilog.Enrichers.AspNetCore.RequestHeader --version 1.0.3
NuGet\Install-Package Serilog.Enrichers.AspNetCore.RequestHeader -Version 1.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="Serilog.Enrichers.AspNetCore.RequestHeader" Version="1.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Serilog.Enrichers.AspNetCore.RequestHeader --version 1.0.3
#r "nuget: Serilog.Enrichers.AspNetCore.RequestHeader, 1.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 Serilog.Enrichers.AspNetCore.RequestHeader as a Cake Addin
#addin nuget:?package=Serilog.Enrichers.AspNetCore.RequestHeader&version=1.0.3

// Install Serilog.Enrichers.AspNetCore.RequestHeader as a Cake Tool
#tool nuget:?package=Serilog.Enrichers.AspNetCore.RequestHeader&version=1.0.3

Serilog Enrichers for AspNetCore 8

NuGet NuGet

Enrich log with AspNetCore 8 request headers data.

Features

Focus to retrieve common and custom headers from HTTP request. This includes:

  • WithHeaderCorrelationId: Enrich with X-Correlation-ID header or custom key as you want. Able to generate new correlation id if not found.
    • keyName: Header key name for CorrelationId.
    • propertyName: Property name to enrich. Default is CorrelationId.
    • generatedWhenNotExist: Generate new correlation id and added to HTTP header if not fond. this generate by Guid.NewGuid() method. Default is true.
    • generatedFormat: Format of generated correlation id. Default is N. more information see Guid.ToString(String).
  • WithHeaderKey: Enrich with custom header key.
    • keyName: Header key name.
    • propertyName: Proerty name to enrich. If not set, keyName will be used.

Usage

Apply enricher to LoggerConfiguration:

Log.Logger = new LoggerConfiguration()
	.Enrich.WithHeaderCorrelationId(
		keyName: "X-Correlation-ID",
		generatedWhenNotExist: true,
		generatedFormat: "N")
	.Enrich.WithHeaderKey(
		keyName: "CustomKey",
		propertyName: "CustomProperty")
	.CreateLogger();

another way, you can apply in appsettings.json file with Serilog.Settings.Configuration package:

{
  "Serilog": {
	"Using": [ "Serilog.Enrichers.AspNetCore" ],
    "Enrich": [
      {
        "Name": "WithHeaderCorrelationId",
        "Args": {
          "keyName": "X-Correlation-ID",
          "generatedWhenNotExist": true,
          "generatedFormat": "N"
        }
      },
      {
        "Name": "WithHeaderKey",
        "Args": {
          "keyName": "CustomKey",
          "propertyName": "CustomProperty"
        }
      },
      {
        "Name": "WithHeaderKey",
        "Args": {
          "keyName": "SameKeyNProperty"
        }
      }
    ]
  }
}

Then add propertyName to outputTemplate:

{
  "Serilog": {
	"WriteTo": [
	  {
		"Name": "Console",
		"Args": {
		  "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}][{CorrelationId}]{CustomProperty|SameKeyNProperty} {Message:lj} {Properties:j}{NewLine}"
		}
	  }
	]
  }
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.3 112 3/2/2024
1.0.2 91 2/18/2024