Serilog enrich with client IP
I wanted to include client IP in my logs but it didn't show up:
builder.Host.UseSerilog((context, loggerConfig) =>
{
loggerConfig.ReadFrom.Configuration(context.Configuration)
.Enrich.WithCorrelationId()
.Enrich.WithRequestHeader("X-Real-IP", "ClientIp");
});
There was a couple of reasons. First, my application was behind a reverse proxy and I had to forward the actual IP to my downstream server. I did this on nginx using following confi:
proxy_set_header X-Real-IP $remote_addr;
I was also missing to add HTTP context accessor to my services:
builder.Services.AddHttpContextAccessor();