Serilog - Remove Message from LogEvent column in SQL

47 views Asked by At

I am using Serilog for AspNetCore Web Api with MSSqlServer sink. I did remove column "Properties" and add "LogEvent" to have a output in Json. Now I have Message in own column but I also have it in LogEvent column in Json. I am trying to remove Message from Json column LogEvent so it is not saved twice. (Or at least have it only in Json, but not in own column)

I did try to "removeStandardColumns" ["Message"] and it removes it from both, own column and LogEvent column. So I tried to add it like customColumns, that doesnt work, Message column is null.

{
  "Name": "MSSqlServer",
  "Args": {
    "connectionString": "XYZ",
    "tableName": "ZZZ",
    "autoCreateSqlTable": true,
    "columnOptionsSection": {
      "addStandardColumns": [ "LogEvent" ],
      "removeStandardColumns": [ "Properties", "MessageTemplate","Message" ],
      "customColumns": [
        {
          "ColumnName": "Message",
          "DataType": "nvarchar"
        }
      ]
    }
  }
}

I also tried to add: .Enrich.With(new RemovePropertiesJsonLogEvent()) With this class implementation:

public class RemovePropertiesJsonLogEvent : ILogEventEnricher
{
    public void Enrich(LogEvent le, ILogEventPropertyFactory lepf)
    {
        le.RemovePropertyIfPresent("SourceContext");
        le.RemovePropertyIfPresent("RequestId");
        le.RemovePropertyIfPresent("RequestPath");
        le.RemovePropertyIfPresent("ConnectionId");
        le.RemovePropertyIfPresent("Message");
        
    }
}

And this appsettings:

"columnOptionsSection": {
  "addStandardColumns": [ "LogEvent" ],
  "removeStandardColumns": [ "Properties", "MessageTemplate" ]
}

But this also doesnt work to remove Message, it is only removing properties inside "Properties": {}.

I found only options to write custom json formatter with this example: text

But thats only last options, I am not sure about performance impact and I hope there is better and easier way to remove "Message" only from Json in LogEvent but keep it in own column "Message"

0

There are 0 answers