I have some problems converting JSON.NET generated json string to BsonDocument using the method that is suggested here: Convert string into MongoDB BsonDocument. I am bulding a MongoDbLog4net appender which inserts LogMessages againt the mongodb. Those messages can contain exceptions and in some cases the exception object serializes to json string that contains points '.' in some keys causing the BsonSerializer.Desrialize method to complain. Is there an easy/efficient way to tell JsonConvert to not put or replace invalid characters with something else?
protected override void Append(LoggingEvent loggingEvent)
{
// the log message here is used to filter and collect the
// fields from loggingEvent we are interested in
var logMessage = new LogMessage(loggingEvent);
// since mongodb does not serialize exceptions very well we
// will use JSON.NET to serialize the LogMessage instance
// and build the BSON document from it
string jsonLogMessage = JsonConvert.SerializeObject(logMessage);
var bsonLogMessage = BsonSerializer.Deserialize<BsonDocument>(jsonLogMessage);
this.logCollection.Insert(bsonLogMessage);
}