Serilog.Enrichers.ClientInfo
1.2.0
dotnet add package Serilog.Enrichers.ClientInfo --version 1.2.0
NuGet\Install-Package Serilog.Enrichers.ClientInfo -Version 1.2.0
<PackageReference Include="Serilog.Enrichers.ClientInfo" Version="1.2.0" />
paket add Serilog.Enrichers.ClientInfo --version 1.2.0
#r "nuget: Serilog.Enrichers.ClientInfo, 1.2.0"
// Install Serilog.Enrichers.ClientInfo as a Cake Addin
#addin nuget:?package=Serilog.Enrichers.ClientInfo&version=1.2.0
// Install Serilog.Enrichers.ClientInfo as a Cake Tool
#tool nuget:?package=Serilog.Enrichers.ClientInfo&version=1.2.0
serilog-enrichers-clientinfo 
Enrich logs with client IP and UserAgent.
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.WithClientAgent()
// ...other configuration...
.CreateLogger();
or in appsettings.json
file:
{
"Serilog": {
"MinimumLevel": "Debug",
"Using": [ "Serilog.Enrichers.ClientInfo" ],
"Enrich": [ "WithClientIp", "WithClientAgent"],
"WriteTo": [
{ "Name": "Console" }
]
}
}
The WithClientIp()
enricher will add a ClientIp
property and the WithClientAgent()
enricher will add a ClientAgent
property to produced events.
For ClientIp
enricher you can configure the X-forwarded-for
header if the proxy server uses a different header to forward IP address.
Log.Logger = new LoggerConfiguration()
.Enrich.WithClientIp("CF-Connecting-IP")
...
{
"Serilog": {
"MinimumLevel": "Debug",
"Using": [ "Serilog.Enrichers.ClientInfo" ],
"Enrich": [
"WithClientAgent",
{
"Name": "WithClientIp",
"Args": {
"xForwardHeaderName": "CF-Connecting-IP"
}
}
],
}
}
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} {ClientIp} {ClientAgent}] {Message:lj}{NewLine}{Exception}")
.Enrich.WithClientIp()
.Enrich.WithClientAgent()
.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 |
---|---|
.NET | net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows |
.NET Core | netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1 |
.NET Standard | netstandard2.0 netstandard2.1 |
.NET Framework | net461 net462 net463 net47 net471 net472 net48 net481 |
MonoAndroid | monoandroid |
MonoMac | monomac |
MonoTouch | monotouch |
Tizen | tizen40 tizen60 |
Xamarin.iOS | xamarinios |
Xamarin.Mac | xamarinmac |
Xamarin.TVOS | xamarintvos |
Xamarin.WatchOS | xamarinwatchos |
-
.NETFramework 4.6.2
- Serilog (>= 2.4.0)
-
.NETStandard 2.0
- Microsoft.AspNetCore.Http (>= 2.1.1)
- Serilog (>= 2.7.1)
-
.NETStandard 2.1
- Microsoft.AspNetCore.Http (>= 2.2.2)
- Serilog (>= 2.9.0)
NuGet packages (21)
Showing the top 5 NuGet packages that depend on Serilog.Enrichers.ClientInfo:
Package | Downloads |
---|---|
KAM.Aether.SharedLibraries
KAM.Aether.SharedLibraries and SharedModels |
|
SuchenExpert.Core.Common
This package for SuchenExpert projects |
|
Kourosh.Aether.SharedLibraries
Kourosh.Aether.SharedLibraries |
|
CodeZero
CodeZero is a set of common implementations to help you implementing Clean Architecture, DDD, CQRS, Specification Patterns and another facilities for new modern web applications is an open-source project written in .NET Core. |
|
Farsica.Framework
Asp.net Core Application Framework |
GitHub repositories (2)
Showing the top 2 popular GitHub repositories that depend on Serilog.Enrichers.ClientInfo:
Repository | Stars |
---|---|
neozhu/CleanArchitectureWithBlazorServer
This is a repository for creating a Blazor Server dashboard application following the principles of Clean Architecture
|
|
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!!
|
- Configure `X-forward-header` for client IP enricher
- Drop support.NET 4.5.2