ChangemakerStudios / serilog-sinks-mongodb

A sink for Serilog that writes events to MongoDB
Apache License 2.0
92 stars 53 forks source link

Is it possible to add custom column? #51

Closed kad1r closed 2 years ago

kad1r commented 4 years ago

Hello, I'm storing modifications on the records as a change logs on mongodb. I'm wondering if it's possible to add custom columns? Or I want to store my changes as a json object not string. Because I don't want to do json parse on string variable. It's available on sql server but I couldn't find any documentation on mongo side.

image

Thank you.

ahmadmujtaba commented 3 years ago

Hi I am facing same problem for Serilog.Sinks.MongoDB. I want to remove standard column like 'Properties' and add some custom columns. Below configuration is working for the SQL but its not working for Mongo DB.

App.Setting.json

{ "Serilog": { "Using": [ "Serilog.Sinks.MSSqlServer", "Serilog.Sinks.MongoDB" ], "MinimumLevel": "Debug", "WriteTo": [ "Console", { "Name": "MSSqlServer", "Args": { "connectionString": "Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=SerilogTest;Integrated Security=True;", "tableName": "Logs", "autoCreateSqlTable": true, "columnOptionsSection": { "removeStandardColumns": [ "Properties" ], "customColumns": [ { "ColumnName": "EventType", "DataType": "int", "AllowNull": false }, { "ColumnName": "TestColumn", "DataType": "int", "AllowNull": false }, { "ColumnName": "Release", "DataType": "varchar", "DataLength": 32 } ] } } }, { "Name": "MongoDB", "Args": { "databaseUrl": "mongodb://localhost/logs", "collectionName": "Logs", "restrictedToMinimumLevel": "Information", "columnOptionsSection": { "removeStandardColumns": [ "Properties" ], "customColumns": [ { "ColumnName": "EventType", "DataType": "int" }, { "ColumnName": "TestColumn", "DataType": "int" }, { "ColumnName": "Release", "DataType": "string"

          }     ]  
    }   
 }  }    ]

} }

Jaben commented 3 years ago

With the latest version, you can add the columns to the output for the logger for the MongoDBBson() sink (using a custom object for example):

Log.ForContext("MyCustomObject", new { Hi = 1, Custom = 2 }, true).Debug("Testing");

This outputs into the Mongo like so:

image

Let me know if this is still an issue @kad1r and @ammachado -- thanks.

Jaben commented 2 years ago

Closing this due to inactivity.