Serilog.Enrichers.ClientInfo
2.1.2
dotnet add package Serilog.Enrichers.ClientInfo --version 2.1.2
NuGet\Install-Package Serilog.Enrichers.ClientInfo -Version 2.1.2
<PackageReference Include="Serilog.Enrichers.ClientInfo" Version="2.1.2" />
paket add Serilog.Enrichers.ClientInfo --version 2.1.2
#r "nuget: Serilog.Enrichers.ClientInfo, 2.1.2"
// Install Serilog.Enrichers.ClientInfo as a Cake Addin #addin nuget:?package=Serilog.Enrichers.ClientInfo&version=2.1.2 // Install Serilog.Enrichers.ClientInfo as a Cake Tool #tool nuget:?package=Serilog.Enrichers.ClientInfo&version=2.1.2
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": "User-Agent"}
}
],
"WriteTo": [
{ "Name": "Console" }
]
}
}
ClientIp
ClientIp
enricher reads client IP from HttpContext.Connection.RemoteIpAddress
. Since version 2.1, for security reasons, it no longer reads the x-forwarded-for
header. To handle forwarded headers, configure ForwardedHeadersOptions. If you still want to log x-forwarded-for
, you can use the RequestHeader
enricher.
Log.Logger = new LoggerConfiguration()
.Enrich.WithClientIp()
...
or
{
"Serilog": {
"MinimumLevel": "Debug",
"Using": [ "Serilog.Enrichers.ClientInfo" ],
"Enrich": [
{
"Name": "WithClientIp"
}
],
}
}
CorrelationId
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": [
{
"Name": "WithCorrelationId",
"Args": {
"headerName": "correlation-id"
"addValueIfHeaderAbsence": true
}
}
],
}
}
RequestHeader
You can use multiple WithRequestHeader
to log different request headers. WithRequestHeader
accepts two parameters; The first parameter headerName
is the header name to log
and the second parameter is propertyName
which is the log property name.
Log.Logger = new LoggerConfiguration()
.Enrich.WithRequestHeader(headerName: "header-name-1")
.Enrich.WithRequestHeader(headerName: "header-name-2", propertyName: "SomeHeaderName")
...
or
{
"Serilog": {
"MinimumLevel": "Debug",
"Using": [ "Serilog.Enrichers.ClientInfo" ],
"Enrich": [
{
"Name": "WithRequestHeader",
"Args": {
"headerName": "User-Agent"
}
},
{
"Name": "WithRequestHeader",
"Args": {
"headerName": "Connection"
}
},
{
"Name": "WithRequestHeader",
"Args": {
"headerName": "Content-Length",
"propertyName": "RequestLength"
}
}
],
}
}
Note
To include logged headers in OutputTemplate
, the header name without -
should be used if you haven't set the log property name. For example, if the header name is User-Agent
, you should use UserAgent
.
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.Enrich.WithRequestHeader("User-Agent")
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss}] {Level:u3} {UserAgent} {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")
.Enrich.WithRequestHeader("another-header-name", "SomePropertyName")
.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();
// ...
}
}
}
Product | Versions 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 is compatible. 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 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. |
NuGet packages (41)
Showing the top 5 NuGet packages that depend on Serilog.Enrichers.ClientInfo:
Package | Downloads |
---|---|
SuchenExpert.Core.Common
This package for SuchenExpert projects |
|
Farsica.Framework
Asp.net Core Application Framework |
|
KAM.Aether.SharedLibraries
KAM.Aether.SharedLibraries and SharedModels |
|
FFCEI.Microservices
A free library for ASP.NET Core 6+ Microservices development, with Model, Model Repository, Entity Framework Core and common Web Api features like CORS, Json serialization fixes, Swagger generation, JWT Authentication for simple and objective microservices development |
|
IntelWash.BaseAPI.Common
Package Description |
GitHub repositories (4)
Showing the top 4 popular GitHub repositories that depend on Serilog.Enrichers.ClientInfo:
Repository | Stars |
---|---|
featbit/featbit
A feature flags service written in .NET
|
|
CodeMazeBlog/CodeMazeGuides
The main repository for all the Code Maze guides
|
|
neozhu/visitormanagement
helps in managing visitors visiting the institutions for various reasons. It allows visitors to check-in digitally to eliminate the tedious registeration and other paperwork. Additionally, it also keeps a track of every individual inside the campus and their timings. Institutions has guards who enter their detail in some notebooks to keep a log which are practically impossible to reconcile. It is really unpleasent and hectic for visitor to stand at the gate and give details about the visit. To ease the process of registeration, Entry-In, Entry-Out, time tracking and logging the history, this VMS can be of great use!!
|
|
Corsinvest/cv4pve-admin
Corsinvest for Proxmox VE Web Admin Clusters - vCenter but for Proxmoxm VE
|
Version | Downloads | Last updated | |
---|---|---|---|
2.1.2 | 250,888 | 10/2/2024 | |
2.1.1 | 671,901 | 8/2/2024 | |
2.1.0 | 202,114 | 7/18/2024 | |
2.0.3 | 2,367,090 | 11/1/2023 | |
2.0.2 | 2,419 | 10/31/2023 | |
2.0.1 | 474,341 | 9/1/2023 | |
2.0.0 | 462,393 | 6/30/2023 | |
1.3.0 | 586,474 | 6/1/2023 | |
1.2.0 | 3,191,255 | 6/26/2022 | |
1.1.4 | 2,326,173 | 8/22/2021 | |
1.1.4-dev01 | 1,055 | 8/22/2021 | |
1.1.3 | 1,697,569 | 12/18/2020 | |
1.1.2 | 128,449 | 12/7/2020 | |
1.1.1 | 1,431 | 12/6/2020 | |
1.1.0 | 140,447 | 4/23/2020 | |
1.0.0 | 22,035 | 1/5/2020 |
Update documentation.