'How to get Serilog to enrich logs

I've set Serilog to read its config from appsettings.json:

return WebHost.CreateDefaultBuilder(args)
   .UseSerilog((ctx, config) => { config.ReadFrom.Configuration(ctx.Configuration); })

The appsettings.json has the following relevant info that specifies enrichers:

{
  "Serilog": {
    "WriteTo": [
      {
        "Name": "RollingFile",
        "Args": {
          "pathFormat": "%WIDGETSAPIBASEDIR%\\logs\\log-{Date}.txt"
        }
      },
      {
        "Name": "Debug"
      }
    ],
    "Enrich": [ "CorrelationId" ]
  }
}

The resulting log doesn't contain any of the data specified in the Enrich property.

I've imported Serilog.Enrichers.CorrelationId, but still get nothing.

I've also tried "Enrich": [ "WithCorrelationId" ]. I've also tried other enrichers ("FromLogContext", "WithMachineName", "WithThreadId"), but still get nothing.

What am I missing?



Solution 1:[1]

Here is an example of my config file that works. Notice I have output templates with the enrichment ?

 "Serilog": {
    "MinimumLevel": {
      "Default": "Debug",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning"
      }
    },
    "WriteTo": [
      {
        "Name": "Async",
        "Args": {
          "configure": [
            {
              "Name": "File",
              "Args": {
                "path": ".//Logs//app.log-.txt",
                "rollingInterval": "Day",
                "rollOnFileSizeLimit": true,
                "outputTemplate": "[{Timestamp :HH:mm:ss} {Level:u3} {SourceContext, -20} {ProcessId} {ProcessName} {ThreadId}] {Message}\n{Exception}",
            ,
                "shared": true
              }
            },
            {
              "Name": "Console"
            }
          ]
        }
      },
      {
        "Name": "SpectreConsole",
        "Args": {
          "outputTemplate": "[{Timestamp:HH:mm:ss} [{Level:u3} {ProcessId}] {Message:lj}{NewLine}{Exception}",
          "minLevel": "Verbose"
        }
      }
    ],
    "Enrich": [
      "FromLogContext",
      "WithMemoryUsage",
      "WithProcessId",
      "WithProcessName",
      "WithThreadId",
      "WithThreadName"
    ]
  }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user18736152