serilog-contrib / serilog-enrichers-clientinfo

Enrich logs with client IP, correlation id and HTTP request headers.
MIT License
99 stars 22 forks source link

ClientAgent replacement not working #23

Closed Su-s closed 1 year ago

Su-s commented 1 year ago

Hi this is my serilog configuration in app.settings.json


{
  "Serilog": {
    "Using": [
      "Serilog.Sinks.Console",
      "Serilog.Sinks.File",
      "Serilog.Sinks.Seq",
      "Serilog.Enrichers.ClientInfo",
      "Serilog.Enrichers.CorrelationId",
      "Serilog.Enrichers.ExceptionStackTraceHash",
      "Serilog.Enrichers.Environment",
      "Serilog.Enrichers.Memory"
    ],
    "MinimumLevel": {
      "Default": "Verbose",
      "Override": {
        "Microsoft.AspNetCore": "Warning",
        "Microsoft": "Information",
        "Microsoft.EntityFrameworkCore.Database.Command": "Error",
        "Microsoft.AspNetCore.SignalR": "Information",
        "Microsoft.AspNetCore.Http.Connections": "Information"
      }
    },
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [correlationId: {CorrelationId} machineName:{MachineName} environmentName:{EnvironmentName} environmentUserName:{EnvironmentUserName} clientIp:{ClientIp} requestHeader:{RequestHeader} processId:{ProcessId} processName:{ProcessName} memory:{MemoryUsage} level:{Level: u3}] ({SourceContext}) {Message} {NewLine} {Exception} {NewLine}",
          "theme": "Serilog.Sinks.SystemConsole.Themes.Literate",
          "restrictedToMinimumLevel": "Information"
        }
      },
      {
        "Name": "Seq",
        "Args": {
          "serverUrl": "http://localhost:5341/",
          "apiKey": "none",
          "restrictedToMinimumLevel": "Information"
        }
      },
      {
        "Name": "File",
        "Args": {
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [correlationId: {CorrelationId} machineName:{MachineName} environmentName:{EnvironmentName} environmentUserName:{EnvironmentUserName} clientIp:{ClientIp} requestHeader:{RequestHeader} processId:{ProcessId} processName:{ProcessName} memory:{MemoryUsage} level:{Level: u3}] ({SourceContext}) {Message} {NewLine} {Exception} {NewLine}",
          "path": "C:\\Users\\Lenovo\\AppData\\Local\\Logs\\Cloud.log",
          "rollingInterval": "Day",
          "restrictedToMinimumLevel": "Verbose"
        }
      }
    ],
    "Enrich": [
      "FromLogContext",
      "WithMachineName",
      {
        "Name": "WithClientIp",
        "Args": {
          "headerName": "CF-Connecting-IP"
        }
      },
      {
        "Name": "WithRequestHeader",
        "Args": {
          "headerName": "User-Agent"
        }
      },
      "WithMessageTemplate",
      "WithEnvironmentUserName",
      "WithMemoryUsage",
      "WithEnvironmentName",
      "WithExceptionDetails",
      "ExceptionStackTraceHash",
      "WithExceptionProperties",
      "WithProcessName",
      "WithProcessId"
    ]
  }
}

I have updated the client agent to request header according to the enricher package change. But now, im not able to display the client agent details in log.

image image

Please help me to fix the issue

mo-esmp commented 1 year ago

@Su-s please replace requestHeader:{RequestHeader} with clientAgent:{UserAgent} and let me know the result.

image

Su-s commented 1 year ago

image

by keeping the changes in the enriched, correct?

now also the log did not changed

image

also the serilog is not showing the clientagent details in seq

image

mo-esmp commented 1 year ago

I added a sample project. Check the branch and see if it is working, use the enricher configuration in the appsettings.json file and also make sure your requests has the User-Agent header.

Su-s commented 1 year ago

image the agent and correlation id are working with your sample code.

image

but I had updated the code slightly so that it match with the settings in my application, and found that the after applying the change to set the log level no logs were displaying after the application start

"MinimumLevel": { "Default": "Verbose", "Override": { "Microsoft.AspNetCore": "Warning", "Microsoft": "Information", "Microsoft.EntityFrameworkCore.Database.Command": "Error", "Microsoft.AspNetCore.SignalR": "Information", "Microsoft.AspNetCore.Http.Connections": "Information" } },

image the logs in the above screenshot are the only logs getting in appliaction

mo-esmp commented 1 year ago

When you run your application for the first time (application startup), these fields are null (first 4 lines of logs) and when client sends request to your application, they should have values. Send a request from your browser and check the logs again.

Su-s commented 1 year ago

yes, understood that. i have updated the serilog wrodaroud app to make the changes work

https://github.com/Su-s/serilogworkaround/commit/d5673b0206d20de17c19ceb5c6b54fa01f5885bc

could you please take a look and suggest me the changes.

when i did the sample without additional configuration, just like the code in your sample app, there were no issues. but its not working as expected, like as before. even after adding the changes you mentioned in the sample app

image

Su-s commented 1 year ago

image when i try to debug the httpcontext become null, please check the image

meanwhile the same on your sample application

image

like this

image

mo-esmp commented 1 year ago

I've added builder.Services.AddHttpContextAccessor(); to your code and it fixed the problem. image

Su-s commented 1 year ago

Thanks for the help made it work!

Fully working example

https://github.com/Su-s/serilogworkaround