Paulo Morgado
Paulo Morgado

Reputation: 14856

Is there a way to enrich the log with a property from configuration?

I know I can add enrichers this way:

{
  "Serilog": {
    "Using": [
      "Serilog",
      "Serilog.Enrichers.Environment",
      "Serilog.Enrichers.Process",
      ...
    ],
    "Enrich": [
      "FromLogContext",
      "WithMachineName",
      "WithProcessId"
    ]
...

Can I do the same for a property (ApplicationName)?

Upvotes: 2

Views: 3337

Answers (1)

Paulo Morgado
Paulo Morgado

Reputation: 14856

One way of doing this is with properties. But the configuration for enrichment properties has a caveat that it's a top level configuration and not an enrichment configuration:

{
  "Serilog": {
    "Using": [
      "Serilog",
      "Serilog.Enrichers.Environment",
      "Serilog.Enrichers.Process",
      ...
    ],
    "Properties": {
      "ApplicationName": "my application"
    },
    "Enrich": [
      "FromLogContext",
      "WithMachineName",
      "WithProcessId"
    ]
...

This will cause the configuration reader to call something like this:

loggerConfiguration.Enrich.WithProperty("ApplicationName", "my application");

Upvotes: 7

Related Questions