Skip to content

Commit 402ee35

Browse files
authored
Update README.md
1 parent 1811e13 commit 402ee35

File tree

1 file changed

+84
-1
lines changed

1 file changed

+84
-1
lines changed

README.md

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,85 @@
1-
# serilog-enrichers-clientinfo
1+
# serilog-enrichers-clientinfo [![NuGet](http://img.shields.io/nuget/v/Serilog.Enrichers.ClientInfo.svg?style=flat)](https://www.nuget.org/packages/Serilog.Enrichers.ClientInfo/)
22
Enrich logs with client IP and UserAgent.
3+
4+
Install the _Serilog.Enrichers.ClientInfo_ [NuGet package](https://www.nuget.org/packages/Serilog.Enrichers.ClientInfo/)
5+
6+
```powershell
7+
Install-Package Serilog.Enrichers.ClientInfo
8+
```
9+
or
10+
```shell
11+
dotnet add package Serilog.Enrichers.ClientInfo
12+
```
13+
14+
Apply the enricher to your `LoggerConfiguration` in code:
15+
16+
```csharp
17+
Log.Logger = new LoggerConfiguration()
18+
.Enrich.WithClientIp()
19+
.Enrich.WithClientAgent()
20+
// ...other configuration...
21+
.CreateLogger();
22+
```
23+
24+
or in `appsttings.json` file:
25+
```json
26+
{
27+
"Serilog": {
28+
"MinimumLevel": "Debug",
29+
"WriteTo": [
30+
{ "Name": "Console" },
31+
{ "Name": "File", "Args": { "path": "%TEMP%\\Logs\\serilog-configuration-sample.txt" } }
32+
],
33+
"Using": [ "Serilog.Enrichers" ],
34+
"Enrich": [ "WithClientIp", "WithClientAgent"],
35+
}
36+
}
37+
```
38+
39+
The `WithClientIp()` enricher will add a `ClientIp` property and the `WithClientAgent()` enricher will add a `ClientAgent` property to produced events.
40+
41+
## Installing into an ASP.NET Core Web Application
42+
You need to register the `IHttpContextAccessor` singleton so the enrichers have access to the requests `HttpContext` to extract client IP and client agent.
43+
This is what your `Startup` class should contain in order for this enricher to work as expected:
44+
45+
```cs
46+
using Microsoft.AspNetCore.Builder;
47+
using Microsoft.AspNetCore.Hosting;
48+
using Microsoft.AspNetCore.Http;
49+
using Microsoft.Extensions.DependencyInjection;
50+
using Microsoft.Extensions.Logging;
51+
using Serilog;
52+
53+
namespace MyWebApp
54+
{
55+
public class Startup
56+
{
57+
public Startup()
58+
{
59+
Log.Logger = new LoggerConfiguration()
60+
.MinimumLevel.Debug()
61+
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3} {ClientIp} {ClientAgent}] {Message:lj}{NewLine}{Exception}")
62+
.Enrich.WithClientIp()
63+
.Enrich.WithClientAgent()
64+
.CreateLogger();
65+
}
66+
67+
// This method gets called by the runtime. Use this method to add services to the container.
68+
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
69+
public void ConfigureServices(IServiceCollection services)
70+
{
71+
// ...
72+
services.AddHttpContextAccessor();
73+
// ...
74+
}
75+
76+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
77+
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
78+
{
79+
// ...
80+
loggerFactory.AddSerilog();
81+
// ...
82+
}
83+
}
84+
}
85+
```

0 commit comments

Comments
 (0)