How to modify LogEvent property values using serilog Enrichers?

Viewed 331

I need to modify specific property value (compress stacktrace string of exception) of object logged using serilog.

The JSON message that is getting logged has following format :

{
  "@l": "Error",
  "errorDetails": {
    "StatusCode": 500,
    "Message": "some exception message.",
    "Exception": {
      "Message": "some exception message.",
      "Data": [],
      "InnerException": null,
      "StackTrace": "STACKTRACE VALUE THAT NEEDS TO BE COMPRESSED",
      "HelpLink": null,
      "Source": "",
      "HResult": -2146233088,
      "$type": "Exception"
    },
    "TimeStamp": 1623771938973,
    "Path": null,
    "$type": "Container"
  }
}

Below is the code for Enricher :

public class CustomEnricher : ILogEventEnricher
    {
        public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
        {
            var errorContainer = logEvent.Properties["errorDetails"];
        }
    } 

In the above code, i m getting the log message in errorContainer (LogEventPropertyValue type).

But not able to access the particular property which i want to modify (stackTrace in Exception).

Basically im logging the exception and want to compress stackTrace property of exception before logging.

I tried modifying the value of stacktrace directly in Exception but its read only.

Can a property be modified using Enricher ?

Is there any alternate solution for this ?

0 Answers
Related