0

I'm trying to show request and response in logs using Serilog in .Net Core MVC 6. But also I need to show the user's informations. How can I show it I've never used logging before?

Program.cs

var logger = new LoggerConfiguration()
    .ReadFrom.Configuration(builder.Configuration)
    .Enrich.FromLogContext()
  
    .CreateLogger();
builder.Host.UseSerilog(logger);

appsettings.json

  "Serilog": {
    "Using": [ "Serilog.Sinks.File" ],
    "MinimumLevel": {
      "Default": "Information"
    },
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "../Logs/webapi-.log",
          "rollingInterval": "Hour",
          "outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {CorrelationId} {Level:u3} {Username} {Message:lj}{NewLine}{Exception}]"
        }
      }
    ]
  }

I can create a log file but how can I add the user's ID , name etc in it? Should I create a middleware ?

  • 2
    You get into trouble with the privacy office if you start putting user ID , names and other personal information in the log files....i guess you should check that aspect first.. – Soumen Mukherjee Oct 30 '22 at 16:13
  • And please refer to this for some good information on how to best use Serilog... https://benfoster.io/blog/serilog-best-practices/ – Soumen Mukherjee Oct 30 '22 at 16:16
  • @SoumenMukherjee this is what I need to do. I'll think about it but first I have to learn how to do it. –  Oct 30 '22 at 16:17

0 Answers0