Project is using .Net Core 3.1 WebAPI.
I am trying to log IP address of the client's device to console. I have installed this package Serilog.Enrichers.ClientInfo v1.2.0 and followed their steps. In addition to console, there is a table ErrorLog with column [ClientIp] where I would like to log the IP address. As you can see in the output, both ClientIp and ClientAgent sections are blank. Am I missing something? Also please verify if the syntax is correct.
Here is my Serilog config from appsettings.json
"Serilog": {
"using": [],
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Warning",
"System": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Enrich": [ "FromLogContext", "WithMachineName", "WithClientIp", "WithClientAgent" ],
"WriteTo": [
{
"Name": "Logger",
"Args": {
"configureLogger": {
"WriteTo": [
{
"Name": "Console",
"Args": {
"outputTemplate": "[{Level:u3} {Timestamp:o}] {ClientIp} {ClientAgent} ({SourceContext}) {Message} {NewLine}"
}
}
]
}
}
},
{
"Name": "Logger",
"Args": {
"configureLogger": {
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "@l in ['Error', 'Fatal']"
}
}
],
"WriteTo": [
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "TestDbContext",
"tableName": "ErrorLogs",
"batchPostingLimit": 50,
"autoCreateSqlTable": true,
"columnOptionsSection": {
"removeStandardColumns": [ "MessageTemplate" ],
"customColumns": [
{
"ColumnName": "ClientIp",
"DataType": "VARCHAR",
"AllowNull": true,
"DataLength": 50
//"NonClusteredIndex": true
}
]
}
}
}
]
}
}
}
]
}
Output:
[INF 2022-07-11T20:13:49.0041535+05:30] () APP:API service has
started
[INF 2022-07-11T20:13:49.5737570+05:30]
(Microsoft.Hosting.Lifetime) Now listening on:
"https://localhost:7017"
[INF 2022-07-11T20:13:49.5987755+05:30]
(Microsoft.Hosting.Lifetime) Now listening on:
"http://localhost:5017"
[INF 2022-07-11T20:13:49.6185554+05:30]
(Microsoft.Hosting.Lifetime) Application started. Press Ctrl+C to
shut down.
[INF 2022-07-11T20:13:49.6255005+05:30]
(Microsoft.Hosting.Lifetime) Hosting environment: "Development"
[INF 2022-07-11T20:13:49.6305270+05:30]
(Microsoft.Hosting.Lifetime) Content root path: "****"
[ERR 2022-07-11T20:14:29.2967601+05:30] () Testing exception
[ERR 2022-07-11T20:14:29.3735005+05:30]
(Serilog.AspNetCore.RequestLoggingMiddleware) HTTP "GET"
"/api/Hospitals" responded 500 in 970.4728 ms