Serilog.Enrichers.ClientInfo 2.0.0
serilog-enrichers-clientinfo 
Enrich logs with client IP, Correlation Id and HTTP request headers.
Install the Serilog.Enrichers.ClientInfo NuGet package
Install-Package Serilog.Enrichers.ClientInfo
or
dotnet add package Serilog.Enrichers.ClientInfo
Apply the enricher to your LoggerConfiguration in code:
Log.Logger = new LoggerConfiguration()
.Enrich.WithClientIp()
.Enrich.WithCorrelationId()
.Enrich.WithRequestHeader("Header-Name1")
// ...other configuration...
.CreateLogger();
or in appsettings.json file:
{
"Serilog": {
"MinimumLevel": "Debug",
"Using": [ "Serilog.Enrichers.ClientInfo" ],
"Enrich": [
"WithClientIp",
"WithCorrelationId",
{
"Name": "WithRequestHeader",
"Args": { "headerName": "Cache-Control"}
}
],
"WriteTo": [
{ "Name": "Console" }
]
}
}
For ClientIp enricher you can configure the x-forwarded-for header if the proxy server uses a different header to forward the IP address.
Log.Logger = new LoggerConfiguration()
.Enrich.WithClientIp(headerName: "CF-Connecting-IP")
...
or
{
"Serilog": {
"MinimumLevel": "Debug",
"Using": [ "Serilog.Enrichers.ClientInfo" ],
"Enrich": [
"WithClientAgent",
{
"Name": "WithClientIp",
"Args": {
"headerName": "CF-Connecting-IP"
}
}
],
}
}
For CorrelationId enricher you can:
- Configure the header name and default header name is
x-correlation-id - Set value for correlation id when the header is not available in request header collection and the default value is false
Log.Logger = new LoggerConfiguration()
.Enrich.WithCorrelationId(headerName: "correlation-id", addValueIfHeaderAbsence: true)
...
or
{
"Serilog": {
"MinimumLevel": "Debug",
"Using": [ "Serilog.Enrichers.ClientInfo" ],
"Enrich": [
"WithClientAgent",
{
"Name": "WithCorrelationId",
"Args": {
"headerName": "correlation-id"
"addValueIfHeaderAbsence": true
}
}
],
}
}
You can use multiple WithRequestHeader to log different request headers.
Log.Logger = new LoggerConfiguration()
.Enrich.WithRequestHeader(headerName: "header-name-1")
.Enrich.WithRequestHeader(headerName: "header-name-2")
...
or
{
"Serilog": {
"MinimumLevel": "Debug",
"Using": [ "Serilog.Enrichers.ClientInfo" ],
"Enrich": [
{
"Name": "WithRequestHeader",
"Args": {
"headerName": "Cache-Control"
}
},
{
"Name": "WithRequestHeader",
"Args": {
"headerName": "Connection"
}
}
],
}
}
Note
To include logged headers in OutputTemplate, the header name without - should be used. For example, if the header name is Cache-Contol, you should use CacheContol.
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss}] {Level:u3} {CacheContol} {Message:lj}{NewLine}{Exception}")
Installing into an ASP.NET Core Web Application
You need to register the IHttpContextAccessor singleton so the enrichers have access to the requests HttpContext to extract client IP and client agent.
This is what your Startup class should contain in order for this enricher to work as expected:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
namespace MyWebApp
{
public class Startup
{
public Startup()
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss}] {Level:u3} CLient IP: {ClientIp} Correlation Id: {CorrelationId} header-name: {headername} {Message:lj}{NewLine}{Exception}")
.Enrich.WithClientIp()
.Enrich.WithCorrelationId()
.Enrich.WithRequestHeader("header-name")
.CreateLogger();
}
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddHttpContextAccessor();
// ...
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// ...
loggerFactory.AddSerilog();
// ...
}
}
}
No packages depend on Serilog.Enrichers.ClientInfo.
- WithClientAgent enricher removed, you can use
WithRequestHeader("User-Agent")enricher - WithCorrelationId enricher is added - WithRequestHeader enricher is added
.NET Framework 4.6.2
- Serilog (>= 2.4.0)
.NET Standard 2.0
- Microsoft.AspNetCore.Http (>= 2.1.1)
- Serilog (>= 2.7.1)
.NET Standard 2.1
- Microsoft.AspNetCore.Http (>= 2.2.2)
- Serilog (>= 2.9.0)
| Version | Downloads | Last updated |
|---|---|---|
| 2.8.0 | 2 | 18.12.2025 |
| 2.7.0 | 1 | 18.12.2025 |
| 2.6.0 | 4 | 25.10.2025 |
| 2.6.0-beta1 | 2 | 18.11.2025 |
| 2.5.0 | 3 | 25.10.2025 |
| 2.5.0-beta1 | 1 | 18.12.2025 |
| 2.4.0 | 3 | 16.09.2025 |
| 2.3.0 | 5 | 05.08.2025 |
| 2.2.0 | 8 | 04.07.2025 |
| 2.1.2 | 13 | 19.12.2024 |
| 2.1.1 | 11 | 18.02.2025 |
| 2.1.0 | 10 | 18.02.2025 |
| 2.0.3 | 12 | 18.02.2025 |
| 2.0.2 | 11 | 19.02.2025 |
| 2.0.1 | 10 | 19.02.2025 |
| 2.0.0 | 10 | 11.02.2025 |
| 1.3.0 | 10 | 18.02.2025 |
| 1.2.0 | 12 | 18.02.2025 |
| 1.1.4 | 10 | 17.02.2025 |
| 1.1.4-dev01 | 7 | 18.02.2025 |
| 1.1.3 | 12 | 18.02.2025 |
| 1.1.2 | 10 | 19.02.2025 |
| 1.1.1 | 10 | 18.02.2025 |
| 1.1.0 | 10 | 18.02.2025 |
| 1.0.0 | 10 | 19.02.2025 |